whycxzp
2025-04-30 d19e91bcc4ed9ab08e8be375fb5fc0f7e874a6a9
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);
        //创建sheet4 -- 每天温度, 30天, 取每天零点零分
        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();
@@ -501,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("放电充电电压数据报表");
        //总的列数根据 放电时长和充电时长决定
        //数据准备
@@ -618,14 +642,14 @@
                "工作成员:周素文、李志标、王月灿等");
        XSSFCell cell3Row2 = row1.createCell(3);
        cell3Row2.setCellValue("放电日期:2021.10.30\n" +
                "放电开始时间:10:00\n" +
                "放电结束时间:19:47");
        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 cell4Row2 = row1.createCell(5);
        cell4Row2.setCellValue("放电电流(A):35\n" +
                "放电时长(时.分):600min\n" +
                "放电容量(Ah):300");
        cell4Row2.setCellValue("放电电流(A):"+ Math.abs(battTestInf.getTestCurr())+"\n" +
                "放电时长(时.分):"+testTimeMinutes+"\n" +
                "放电容量(Ah):"+Math.abs(battTestInf.getTestCap()));
        XSSFCell cell5Row2 = row1.createCell(columnCount-1);
        cell5Row2.setCellValue("环境温度(℃):24.5\n" +
@@ -1091,6 +1115,8 @@
            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) {