package com.whyc.service; import com.whyc.dto.ActmStopReason; import com.whyc.pojo.db_lithium_testdata.BattLithiumTestData; import com.whyc.pojo.db_lithium_testdata.BattLithiumTestDataInf; 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; @Autowired(required = false) private BattLithiumTestDataInfService infService; //导出文件 public void exportExcel1(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 list=subService.getTdataById(devId,testRecordCount); if(list!=null){ //数据栏 for (int i = 0; i < list.size(); i++) { sheet.createRow(rowNum); //创建行 BattLithiumTestData tdata=list.get(i); for(int j=0;j list=subService.getTdataByIdWithListA200(devId,testRecordCount); int rowNum=0; // 创建标题行 Row row = sheet.createRow(rowNum); row.setHeightInPoints(20.0f); // 填充数据 for(int i=0;i0){ List monNums=list.get(0).getMonNums(); for (int i=0;i monVols = tdata.getMonVols(); for (int j = 0; j < monVols.size(); j++) { sheet.getRow(rowNum).createCell(6 + j).setCellValue(String.format("%.3f", monVols.get(j))); } } rowNum++; } } try { // 写入到输出流 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); wb.write(byteArrayOutputStream); wb.close(); // 设置响应头 String filename = "设备历史数据"; 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(); } } //测试报告封面 private void exportExcelCoverA200(Integer devId, Integer testRecordCount, Workbook wb) { Sheet sheet = wb.createSheet("测试报告封面"); // 创建一个单元格样式,并设置字体大小为5 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中 Font font = wb.createFont(); font.setFontHeightInPoints((short)20); cellStyle.setFont(font); //获取放电inf BattLithiumTestDataInf tinf=infService.getTinfExport(devId,testRecordCount); String[] titiles33=new String[]{"PACK/模组号:","测试开始时间:","测试结束时间:","数据类型:","停止原因:","预放容量(AH):" ,"放电电流(A):","测试时长(h:m:s):","单串下限(V):","组端下限(V):","电芯串数:","最高温度(°C):","最低温度(°C):","采集频率(秒):"}; String[] titiles34=new String[]{tinf.getBatteryName(),ActionUtil.sdf.format(tinf.getTestStarttime()),ActionUtil.sdf.format(tinf.getRecordTime()), "放电数据",tinf.getStopTypeReason(),String.format("%.1f",tinf.getTestCap()) ,String.format("%.1f",tinf.getTestCurr()),ActionUtil.secToTime(tinf.getTestTimelong()),String.format("%.1f",tinf.getMonVolLow()),String.format("%.1f",tinf.getGroupVolLow()),String.valueOf(tinf.getBatteryNumber()) ,String.format("%.1f",tinf.getMaxTemp()),String.format("%.1f",tinf.getMinTemp()),String.valueOf(tinf.getSaveInverter())}; String[] titiles23=new String[]{"PACK/模组号:","测试开始时间:","测试结束时间:","数据类型:","停止原因:","充电限流(A):" ,"测试时长(h:m:s):","单串上限(V):","电流阀值(A):","电芯串数:","充电限压(V)","最高温度(°C):","最低温度(°C):","采集频率(秒):"}; String[] titiles24=new String[]{tinf.getBatteryName(),ActionUtil.sdf.format(tinf.getTestStarttime()),ActionUtil.sdf.format(tinf.getRecordTime()), "充电数据",tinf.getStopTypeReason(),String.format("%.1f",tinf.getTestCurr()) ,ActionUtil.secToTime(tinf.getTestTimelong()),String.format("%.1f",tinf.getMonVolHigh()),String.format("%.1f",tinf.getTestcurrLimit()),String.valueOf(tinf.getBatteryNumber()) ,String.format("%.1f",tinf.getCharVoltage()),String.format("%.1f",tinf.getMaxTemp()),String.format("%.1f",tinf.getMinTemp()),String.valueOf(tinf.getSaveInverter())}; int rowNum=5; if(tinf.getTestType()==3){//放电 for (int i=0;i list=subService.getTdataByIdWithListActm(devId,testRecordCount); int rowNum=0; // 创建标题行 Row row = sheet.createRow(rowNum); row.setHeightInPoints(20.0f); // 填充数据 for(int i=0;i0){ List monNums=list.get(0).getMonNums(); for (int i=0;i monVols = tdata.getMonVols(); List monCurrs = tdata.getMonCurrs(); List monCaps = tdata.getMonCaps(); List monWhs = tdata.getMonWhs(); List monNums=list.get(i).getMonNums(); for (int j = 0; j < monNums.size(); j++) { sheet.getRow(rowNum).createCell(4*j+1 ).setCellValue(String.format("%.3f", monVols.get(j))); sheet.getRow(rowNum).createCell(4*j+1+1).setCellValue(String.format("%.3f", monCurrs.get(j))); sheet.getRow(rowNum).createCell(4*j+1+2).setCellValue(String.format("%.3f", monCaps.get(j))); sheet.getRow(rowNum).createCell(4*j+1+3).setCellValue(String.format("%.3f", monWhs.get(j))); } rowNum++; } } try { // 写入到输出流 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); wb.write(byteArrayOutputStream); wb.close(); // 设置响应头 String filename = "设备历史数据"; 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(); } } //测试报告封面 private void exportExcelCoverActm(Integer devId, Integer testRecordCount, Workbook wb) { Sheet sheet = wb.createSheet("模组配置"); // 创建一个单元格样式,并设置字体大小为5 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中 Font font = wb.createFont(); font.setFontHeightInPoints((short)20); cellStyle.setFont(font); //获取放电inf BattLithiumTestDataInf tinf=infService.getTinfExport(devId,testRecordCount); String[] titiles3=new String[]{"电池包序号:","模组编号:","工作模式:","电池类型:","电池串数:","电压阀值(v):" ,"工作电流(A):","开始测试时间:","结束测试时间:","停止原因:"}; String[] titiles4=new String[]{String.valueOf(tinf.getBattIdx()+1),tinf.getBatteryName(), ActmStopReason.getTestType(tinf.getTestType()), ActmStopReason.getBattType(tinf.getBattType()),String.valueOf(tinf.getBatteryNumber()),String.format("%.1f",tinf.getVolThreshold()) ,String.format("%.1f",tinf.getTestCurr()), ActionUtil.sdf.format(tinf.getTestStarttime()),ActionUtil.sdf.format(tinf.getRecordTime()), tinf.getStopTypeReason()}; int rowNum=5; for (int i=0;i