whyclxw
2024-09-21 12c0bb25a0540d97e7c3348e31c9b25a62495390
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.whyc.service;
 
import com.whyc.pojo.db_batt_testdata.BatttestdataId;
import com.whyc.util.ActionUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
 
@Service
public class ExcelExportService {
    @Autowired(required = false)
    private SubTablePageInfoService subService;
 
    //导出文件
    public void exportExcel(Integer devId,  Integer testRecordCount,HttpServletResponse response) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("设备历史数据");
        String[] titiles=new String[]{"测试时间","电池组编号","单体编号","测试次数","测试类型","测试容量"
                                    ,"组端电压(V)","测试电流(A)","测试容量(AH)","单体电压(V)","单体温度(℃)"
                                    ,"单体电流(A)","单体容量(AH)","单体能量","单体状态","故障信息"};
        int rowNum=0;
        // 创建标题行
        Row row = sheet.createRow(rowNum);
        // 填充数据
        for(int i=0;i<titiles.length;i++){
            row.createCell(i).setCellValue(titiles[i]);
        }
        rowNum++;
        List<BatttestdataId> list=subService.getTdataById(devId,testRecordCount);
        if(list!=null){
            //数据栏
            for (int i = 0; i < list.size(); i++) {
                sheet.createRow(rowNum);  //创建行
                BatttestdataId tdata=list.get(i);
                for(int j=0;j<titiles.length;j++){
                    sheet.getRow(rowNum).createCell(0 ).setCellValue(ActionUtil.secToTime(tdata.getTestTimelong()));
                    sheet.getRow(rowNum).createCell(1).setCellValue(tdata.getBattIdx()+1);
                    sheet.getRow(rowNum).createCell(2 ).setCellValue(tdata.getMonNum());
                    sheet.getRow(rowNum).createCell(3).setCellValue(tdata.getTestRecordCount());
                    sheet.getRow(rowNum).createCell(4).setCellValue(tdata.getTestType());
                    sheet.getRow(rowNum).createCell(5).setCellValue(tdata.getRecordNum());
                    sheet.getRow(rowNum).createCell(6).setCellValue(tdata.getGroupVol());
                    sheet.getRow(rowNum).createCell(7).setCellValue(tdata.getTestCurr());
                    sheet.getRow(rowNum).createCell(8).setCellValue(tdata.getTestCap());
                    sheet.getRow(rowNum).createCell(9 ).setCellValue(tdata.getMonVol());
                    sheet.getRow(rowNum).createCell(10 ).setCellValue(tdata.getMonTmp());
                    sheet.getRow(rowNum).createCell(11 ).setCellValue(tdata.getMonCurr());
                    sheet.getRow(rowNum).createCell(12 ).setCellValue(tdata.getMonCap());
                    sheet.getRow(rowNum).createCell(13 ).setCellValue(tdata.getMonWh());
                    sheet.getRow(rowNum).createCell(14 ).setCellValue(tdata.getMonState());
                    sheet.getRow(rowNum).createCell(15 ).setCellValue(tdata.getMonFault());
                }
                rowNum++;
            }
        }
        try {
            // 写入到输出流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            workbook.write(byteArrayOutputStream);
            workbook.close();
 
            // 设置响应头
            String filename = "设备历史数据";
            /*response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+
                    new String(filename.getBytes("UTF-8"), "ISO8859-1")+
                    ".xlsx");*/
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode (filename, "utf-8") + ".xlsx");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
 
            // 写入响应流并关闭
            response.getOutputStream().write(byteArrayOutputStream.toByteArray());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}