From 73bb5617e6aeb4ee8f7dfd4ad54b61d477969d86 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期二, 10 六月 2025 20:22:06 +0800
Subject: [PATCH] 自动充放电自动化流程

---
 src/main/java/com/whyc/service/ExcelService.java |  213 +++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 159 insertions(+), 54 deletions(-)

diff --git a/src/main/java/com/whyc/service/ExcelService.java b/src/main/java/com/whyc/service/ExcelService.java
index b78d4e6..2495b18 100644
--- a/src/main/java/com/whyc/service/ExcelService.java
+++ b/src/main/java/com/whyc/service/ExcelService.java
@@ -18,10 +18,12 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -52,22 +54,44 @@
      *
      * @throws IOException
      */
-    public void dcReport(int battGroupId,int testRecordCount) throws IOException {
-        String filePath = "C:\\Users\\29550\\Desktop\\褰撳墠椤圭洰\\2023\\0涔屼笢寰风郴缁焅\娴嬭瘯.xlsx";
+    public void dcReport(int battGroupId, int testRecordCount, HttpServletResponse response) throws IOException {
+        //String filePath = "C:\\Users\\29550\\Desktop\\褰撳墠椤圭洰\\2023\\0涔屼笢寰风郴缁焅\娴嬭瘯.xlsx";
         //鍒涘缓涓�涓伐浣滅翱
         XSSFWorkbook workbook = new XSSFWorkbook();
         //鍒涘缓sheet1
-        createSheet1(workbook,battGroupId,testRecordCount);
+        PowerInf battInfo = createSheet1(workbook, battGroupId, testRecordCount);
         //鍒涘缓sheet2 -- 姣忔湀鐢靛帇,12涓湀,鍙栨瘡鏈�1鍙烽浂鐐归浂鍒�
         //鍒涘缓sheet3 -- 姣忔湀鍐呴樆, 12涓湀,鍙栨瘡鏈�1鍙烽浂鐐归浂鍒�
-        //createSheet2And3(workbook,battGroupId);
+        createSheet2And3(workbook,battGroupId);
         //鍒涘缓sheet4 -- 姣忓ぉ娓╁害, 30澶�, 鍙栨瘡澶╅浂鐐归浂鍒�
-        //createSheet4(workbook,battGroupId);
+        createSheet4(workbook,battGroupId);
 
-        // 灏嗗伐浣滅翱鍐欏叆鏂囦欢
+        /*// 灏嗗伐浣滅翱鍐欏叆鏂囦欢
         FileOutputStream fileOut = new FileOutputStream(filePath);
-        workbook.write(fileOut);
+        workbook.write(fileOut);*/
+        String filename ="鐩存祦鎶ヨ〃_"+battInfo.getPowerName()+"_"+battInfo.getBattGroupName()+".xlsx";
+        // 鏍规嵁娴忚鍣ㄥ鎴风鍋氫笉鍚岀紪鐮侊紝鍏煎鎬ф洿濂�
+        String encodedFilename = java.net.URLEncoder.encode(filename, String.valueOf(StandardCharsets.UTF_8));
+        boolean isMSBrowser = false;
+        String ua = response.getHeader("User-Agent");
+        if (ua != null) {
+            ua = ua.toLowerCase();
+            if (ua.contains("msie") || ua.contains("edge") || ua.contains("trident")) {
+                isMSBrowser = true;
+            }
+        }
 
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        if (isMSBrowser) {
+            // IE/Edge 浣跨敤 UTF-8 缂栫爜骞剁敤 URLEncoder
+            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFilename + ";filename*=utf-8''" + encodedFilename);
+        } else {
+            // Chrome/Firefox 绛変娇鐢� RFC 5987 缂栫爜鏂瑰紡
+            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + encodedFilename);
+        }
+
+
+        workbook.write(response.getOutputStream());
         // 鍏抽棴宸ヤ綔绨�
         workbook.close();
 
@@ -214,6 +238,10 @@
         XSSFCellStyle cellStyleOriginal2 = cellOfSheet3.getCellStyle();
         XSSFCellStyle cellStyleNew2 = getCellStyleFont(cellStyleOriginal2,workbook, true, 15);
         cellOfSheet3.setCellStyle(cellStyleNew2);
+
+        //璁剧疆鏁板�肩被鍨嬩繚鐣欎綅鏁�
+        setDateFormat(workbook,sheet2,3,110,1,columnCount-2, "0.0000");
+        setDateFormat(workbook,sheet3,3,110,1,columnCount-2, "0.0000");
 
 
         // 鍒涘缓缁樺浘宸ュ叿
@@ -465,6 +493,8 @@
             cell.setCellStyle(cellStyle);
         }
 
+        //璁剧疆鏁板�肩被鍨嬪崟鍏冩牸淇濈暀浣嶆暟
+        setDateFormat(workbook,sheet4,3,110,1,columnCount-2, "0.0000");
         createChart(sheet4, columnCount,3,"娓╁害鎶樼嚎鍥�");
 
     }
@@ -495,7 +525,7 @@
     }
 
     //private static void createSheet1(XSSFWorkbook workbook, int battGroupId, int testRecordCount) {
-    private void createSheet1(XSSFWorkbook workbook, int battGroupId, int testRecordCount) {
+    private PowerInf createSheet1(XSSFWorkbook workbook, int battGroupId, int testRecordCount) {
         XSSFSheet sheet = workbook.createSheet("鏀剧數鍏呯數鐢靛帇鏁版嵁鎶ヨ〃");
         //鎬荤殑鍒楁暟鏍规嵁 鏀剧數鏃堕暱鍜屽厖鐢垫椂闀垮喅瀹�
         //鏁版嵁鍑嗗
@@ -572,22 +602,6 @@
         CellRangeAddress region = new CellRangeAddress(0, 0,  0, columnCount-1);
         sheet.addMergedRegion(region);
 
-        //TODO 绗簩琛岀殑鍚堝苟鍒楁槸鏍规嵁鍙樺姩鐨�,鍚庣画璋冩暣
-        CellRangeAddress region21 = new CellRangeAddress(1, 1,  0, 1);
-        sheet.addMergedRegion(region21);
-
-        CellRangeAddress region22 = new CellRangeAddress(1, 1,  3, 9);
-        sheet.addMergedRegion(region22);
-
-        CellRangeAddress region23 = new CellRangeAddress(1, 1,  10, 13);
-        sheet.addMergedRegion(region23);
-
-        CellRangeAddress region24 = new CellRangeAddress(1, 1,  14, 17);
-        sheet.addMergedRegion(region24);
-
-        CellRangeAddress region25 = new CellRangeAddress(1, 1,  18, 24);
-        sheet.addMergedRegion(region25);
-
         //绗笁琛�,绗簲琛�,绗叚琛岀殑鍚堝苟鍒�,鍓嶄袱涓崟鍏冩ā鍧楁槸鍥哄畾鐨�,鍏朵腑1涓悎骞跺崟鍏�
         CellRangeAddress region31 = new CellRangeAddress(2, 2,  1, 2);
         CellRangeAddress region32 = new CellRangeAddress(2, 2,  3, 4);
@@ -611,45 +625,56 @@
         cellRow1.setCellStyle(style);
 
         //绗簩琛屾槸鏀剧數鍩烘湰淇℃伅
+        //鍥犱负鏄姩鎬佺殑,娌″姙娉曞儚妯℃澘涓�鏍�.
+        //钃勭數姹犵粍鍙�:绗竴鍒�
+        //宸ヤ綔绁ㄤ俊鎭�:浜屼笁鍒�
+        //鏀剧數鏃ユ湡:绗洓鍒楄捣姝�-鏀剧數缁撴潫闈欑疆0.5h
+        //鏀剧數鐢垫祦,鏃堕暱,瀹归噺:鏀剧數缁撴潫鍚庨潤缃�0.5h璧峰埌鍏呮弧鐢�
+        //鐜娓╂箍搴�:澶囨敞
         XSSFRow row1 = sheet.createRow(1);
         XSSFCell cell1Row2 = row1.createCell(0);
         cell1Row2.setCellValue("钃勭數姹犵粍鍙�:" + battInfo.getBattGroupName());
 
-        XSSFCell cell2Row2 = row1.createCell(2);
-        cell2Row2.setCellValue("");
 
-        XSSFCell cell3Row2 = row1.createCell(3);
-        cell3Row2.setCellValue("宸ヤ綔绁ㄥ彿锛�2273925\n" +
+        XSSFCell cell2Row2 = row1.createCell(1);
+        cell2Row2.setCellValue("宸ヤ綔绁ㄥ彿锛�2273925\n" +
                 "宸ヤ綔璐熻矗浜猴細椹簯鐕昞n" +
                 "宸ヤ綔鎴愬憳锛氬懆绱犳枃銆佹潕蹇楁爣銆佺帇鏈堢伩绛�");
 
-        XSSFCell cell4Row2 = row1.createCell(10);
-        cell4Row2.setCellValue("鏀剧數鏃ユ湡锛�2021.10.30\n" +
-                "鏀剧數寮�濮嬫椂闂达細10:00\n" +
-                "鏀剧數缁撴潫鏃堕棿锛�19:47");
+        XSSFCell cell3Row2 = row1.createCell(3);
+        cell3Row2.setCellValue("鏀剧數鏃ユ湡锛�"+ DateUtil.YYYY_DOT_MM_DOT_DD.format(dischargeStartTime) +"\n" +
+                "鏀剧數寮�濮嬫椂闂达細"+ DateUtil.YYYY_MM_DD_HH_MM.format(dischargeStartTime) +"\n" +
+                "鏀剧數缁撴潫鏃堕棿锛�"+ DateUtil.YYYY_MM_DD_HH_MM.format(dischargeEndTime.getTime()));
 
-        XSSFCell cell5Row2 = row1.createCell(14);
-        cell5Row2.setCellValue("鏀剧數鐢垫祦锛圓锛夛細35\n" +
-                "鏀剧數鏃堕暱锛堟椂.鍒嗭級锛�600min\n" +
-                "鏀剧數瀹归噺锛圓h锛夛細300");
+        XSSFCell cell4Row2 = row1.createCell(5);
+        cell4Row2.setCellValue("鏀剧數鐢垫祦锛圓锛夛細"+ Math.abs(battTestInf.getTestCurr())+"\n" +
+                "鏀剧數鏃堕暱锛堟椂.鍒嗭級锛�"+testTimeMinutes+"\n" +
+                "鏀剧數瀹归噺锛圓h锛夛細"+Math.abs(battTestInf.getTestCap()));
 
-        XSSFCell cell6Row2 = row1.createCell(18);
-        cell6Row2.setCellValue("鐜娓╁害锛堚剝锛夛細24.5\n" +
+        XSSFCell cell5Row2 = row1.createCell(columnCount-1);
+        cell5Row2.setCellValue("鐜娓╁害锛堚剝锛夛細24.5\n" +
                 "鐜婀垮害锛�%锛夛細46.7");
-        //绗簩琛岃缃姞绮�
-        //setRowStyle(sheet.getRow(1),cellStyleFontBold);
+
+        //浜岃鐨勫悎骞跺垪鏄牴鎹彉鍔ㄧ殑
+        row1.setHeightInPoints(57);
+        sheet.getRow(0).setHeightInPoints(42);
+        CellRangeAddress region22 = new CellRangeAddress(1, 1,  1, 2);
+        sheet.addMergedRegion(region22);
+
+        CellRangeAddress region23 = new CellRangeAddress(1, 1,  3, 4);
+        sheet.addMergedRegion(region23);
+
+        CellRangeAddress region24 = new CellRangeAddress(1, 1,  5, columnCount-2);
+        sheet.addMergedRegion(region24);
 
         //绗笁琛屾槸鐢垫睜鍙峰拰鐢靛帇绛夌浉鍏虫姮澶�
         //绗洓琛屾槸鐢靛帇鍒楀拰娓╁害鍒楁爣璇�
         XSSFRow row2 = sheet.createRow(2);
         XSSFRow row3 = sheet.createRow(3);
-        XSSFRow row4 = sheet.createRow(4);
-        XSSFRow row5 = sheet.createRow(5);
 
         row2.createCell(0).setCellValue("钃勭數姹犲彿锛�#锛�");
         row2.createCell(1).setCellValue("钃勭數姹犳诞鍏呯數鍘嬪�硷紙V锛�");
         row2.createCell(3).setCellValue("鏀剧數鍓嶈搫鐢垫睜寮�璺數鍘嬪�硷紙V锛�");
-        row2.createCell(4);
 
         row3.createCell(0).setCellValue("椤圭洰");
         row3.createCell(1).setCellValue("-1锛堢數鍘嬶級");
@@ -730,7 +755,7 @@
         List<BattRealTimeDataHistory> fcVolListOriginal = battRTDataHisService.getFcVolList(battGroupId,dischargeStartTime);
         //鎸夊崟浣撶紪鍙锋帓搴�,鎸夌紪鍙峰崌搴�
         fcVolListOriginal.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum));
-        addStatistics(fcVolListOriginal,"vol");
+        addStatistics(fcVolListOriginal,"vol","tmp");
         //鎬荤數娴佹�荤數鍘嬪垪鍏�
         Float fcGroupVol = fcVolListOriginal.get(0).getGroupVol();
         Float fcGroupCurr = fcVolListOriginal.get(0).getGroupCurr();
@@ -845,17 +870,13 @@
             //rowTemp.createCell(4).setCellValue("鏀剧數鍓嶈搫鐢垫睜寮�璺俯搴﹀��:"+(j+1)+"#");
             if(j == 0){
                 rowTemp.createCell(1).setCellValue(fcVolList.get(j).getGroupVol());
-                rowTemp.createCell(2);
                 rowTemp.createCell(3).setCellValue(preVolList.get(j).getGroupVol());
-                rowTemp.createCell(4);
             }else if (j == 1){
                 rowTemp.createCell(1).setCellValue(fcVolList.get(j).getGroupCurr());
-                rowTemp.createCell(2);
                 rowTemp.createCell(3).setCellValue(preVolList.get(j).getGroupCurr());
-                rowTemp.createCell(4);
             }else {
                 rowTemp.createCell(1).setCellValue(fcVolList.get(j).getMonVol());
-                rowTemp.createCell(2).setCellValue(fcVolList.get(j).getMonVol());
+                rowTemp.createCell(2).setCellValue(fcVolList.get(j).getMonTmp());
                 rowTemp.createCell(3).setCellValue(preVolList.get(j).getMonVol());
                 rowTemp.createCell(4).setCellValue(preVolList.get(j).getMonTmp());
             }
@@ -1066,6 +1087,16 @@
             rowTemp.createCell(columnIndexTemp);
         }
 
+        //璁剧疆鍓�6琛屾墍鏈夌殑琛ㄦ牸鍒涘缓,閬垮厤鍑虹幇鏃犺竟妗�
+        for (int i = 0; i < 6; i++) {
+            XSSFRow rowTmp = sheet.getRow(i);
+            for (int j = 0; j < columnCount; j++) {
+                if(rowTmp.getCell(j) == null){
+                    rowTmp.createCell(j);
+                }
+            }
+        }
+
         //=====================鏍煎紡璁剧疆=====================//
         //璁剧疆鍒楀
         for (int i = 0; i < columnCount; i++) {
@@ -1075,12 +1106,33 @@
         addGlobalStylesToAllCells(sheet, workbook);
 
         //鍒涘缓绗竴琛屾槸鏍囬琛�
-        setRowStyle(workbook,sheet.getRow(0),true,15);
+        setRowStyle(workbook,sheet.getRow(0),true,20);
         //绗簩琛岃缃姞绮�
-        setRowStyle(workbook,sheet.getRow(1),true,15);
-
+        setRowStyle(workbook,sheet.getRow(1),true,11,HorizontalAlignment.LEFT);
+        //绗洓琛岃缃瓧浣撻鑹�
+        setRowStyle(workbook,sheet.getRow(3),false,9,IndexedColors.GREY_40_PERCENT.getIndex());
+        //绗�5琛屽埌鏈�鍚庝竴琛�,璁剧疆鏁板�肩殑灏忔暟鐐逛负4浣�
+            setDateFormat(workbook,sheet,4,113,1,columnCount-1,"0.0000");
         //鐢熸垚鍥捐〃
         createChart(sheet, columnCount, 6,"");
+
+        return battInfo;
+    }
+
+    private void setDateFormat(Workbook workbook,XSSFSheet sheet, int rowIndexStart, int rowIndexEnd,int columnIndexStart,int columnIndexEnd, String formatStr) {
+        CellStyle decimalStyle = workbook.createCellStyle();
+        DataFormat format = workbook.createDataFormat();
+        decimalStyle.setDataFormat(format.getFormat(formatStr));
+        for (int i = rowIndexStart; i <= rowIndexEnd; i++) {
+            XSSFRow row = sheet.getRow(i);
+            for (int j = columnIndexStart; j <= columnIndexEnd; j++) {
+                XSSFCell cell = row.getCell(j);
+                if(cell.getCellType() == CellType.NUMERIC) {
+                    cell.getCellStyle().setDataFormat(format.getFormat(formatStr));
+                }
+            }
+
+        }
     }
 
     private XSSFCellStyle getCellStyleFont(XSSFCellStyle cellStyleOriginal, XSSFWorkbook workbook, boolean isFontBold, int fontSize) {
@@ -1212,6 +1264,7 @@
             //璁剧疆瀛椾綋鍜屽姞绮�
             Font font = workbook.createFont();
             font.setFontHeightInPoints((short) (fontSize));
+            font.setFontName("瀹嬩綋");
             font.setBold(isFontBold);
 
             cellStyleNew.setFont(font);
@@ -1219,8 +1272,51 @@
         }
     }
 
+    public void setRowStyle(Workbook workbook,Row row,boolean isFontBold,int fontSize,HorizontalAlignment horizontalAlignment) {
+        for (Cell cell : row) {
+            //鍏堝厠闅嗗師鏉ョ殑灞炴��
+            CellStyle cellStyleNew = workbook.createCellStyle();
+            cellStyleNew.cloneStyleFrom(cell.getCellStyle());
+
+            //璁剧疆瀛椾綋鍜屽姞绮�
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints((short) (fontSize));
+            font.setBold(isFontBold);
+            font.setFontName("瀹嬩綋");
+
+            //璁剧疆姘村钩瀵归綈鏂瑰紡
+            cellStyleNew.setAlignment(horizontalAlignment);
+
+            cellStyleNew.setFont(font);
+            cell.setCellStyle(cellStyleNew);
+        }
+    }
+
+    public void setRowStyle(Workbook workbook,Row row,boolean isFontBold,int fontSize,short color) {
+        for (Cell cell : row) {
+            //璺宠繃绗竴鍒�
+            if(cell.getColumnIndex() == 0) {
+                continue;
+            }
+            //鍏堝厠闅嗗師鏉ョ殑灞炴��
+            CellStyle cellStyleNew = workbook.createCellStyle();
+            cellStyleNew.cloneStyleFrom(cell.getCellStyle());
+
+            //璁剧疆瀛椾綋鍜屽姞绮�
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints((short) (fontSize));
+            font.setBold(isFontBold);
+            font.setFontName("瀹嬩綋");
+            font.setColor(color);
+
+            cellStyleNew.setFont(font);
+            cell.setCellStyle(cellStyleNew);
+        }
+    }
+
     /**
-     * 杩欎釜鏂规硶鏈�鍚庢墽琛�,鐢ㄤ簬瀵瑰凡琚垱寤虹殑琛屽唴鐨勬墍鏈夊崟鍏冩牸娣诲姞杈规.鑳借嚜鍔ㄨ瘑鍒鍒涘缓鐨勬墍鏈夊垪鍜屾墍鏈夊崟鍏冩牸
+     * 璁剧疆鍏ㄥ眬鐨勫睘鎬�.杩欎釜鏂规硶鍦ㄦ墍鏈夊崟鍏冩牸鍒涘缓鍚庤皟鐢�
+     * 鐢ㄤ簬瀵瑰凡琚垱寤虹殑琛屽唴鐨勬墍鏈夊崟鍏冩牸娣诲姞杈规.鑳借嚜鍔ㄨ瘑鍒鍒涘缓鐨勬墍鏈夊垪鍜屾墍鏈夊崟鍏冩牸
      * @param sheet
      * @param workbook
      */
@@ -1232,7 +1328,16 @@
         borderedStyle.setBorderBottom(BorderStyle.THIN);
         borderedStyle.setBorderLeft(BorderStyle.THIN);
         borderedStyle.setBorderRight(BorderStyle.THIN);
-
+        Font font = workbook.createFont();
+        //榛樿瀛椾綋澶у皬涓�9,瀹嬩綋
+        font.setFontHeightInPoints((short) 9);
+        font.setFontName("瀹嬩綋");
+        borderedStyle.setFont(font);
+        //榛樿妯珫灞呬腑
+        borderedStyle.setAlignment(HorizontalAlignment.CENTER);
+        borderedStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+        //榛樿鑷姩鎹㈣
+        borderedStyle.setWrapText(true);
         for (Row row : sheet) {
             for (Cell cell : row) {
                 cell.setCellStyle(borderedStyle);

--
Gitblit v1.9.1