From 926a8b4621713460dd5622a5847edecda1e905ea Mon Sep 17 00:00:00 2001 From: whycxzp <glperry@163.com> Date: 星期四, 05 六月 2025 17:26:25 +0800 Subject: [PATCH] 表格生成调整 --- src/main/java/com/whyc/service/ExcelService.java | 565 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 518 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/whyc/service/ExcelService.java b/src/main/java/com/whyc/service/ExcelService.java index 0e39f6f..cbba9d6 100644 --- a/src/main/java/com/whyc/service/ExcelService.java +++ b/src/main/java/com/whyc/service/ExcelService.java @@ -1,11 +1,20 @@ package com.whyc.service; +import com.whyc.mapper.CommonMapper; import com.whyc.pojo.db_batt.PowerInf; import com.whyc.pojo.db_dis_batt.BattTestInf; import com.whyc.pojo.db_power_history.BattRealTimeDataHistory; +import com.whyc.util.DateUtil; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xddf.usermodel.CompoundLine; +import org.apache.poi.xddf.usermodel.XDDFLineProperties; +import org.apache.poi.xddf.usermodel.XDDFShapeProperties; +import org.apache.poi.xddf.usermodel.chart.*; import org.apache.poi.xssf.usermodel.*; +import org.openxmlformats.schemas.drawingml.x2006.chart.*; +import org.openxmlformats.schemas.drawingml.x2006.main.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -29,6 +38,9 @@ //private static BattRealTimeDataHistoryService battRTDataHisService; private BattRealTimeDataHistoryService battRTDataHisService; + @Autowired + private CommonMapper commonMapper; + /** * 灏嗙數姹犵粍鏀剧數鍏呯數鏁版嵁鍐欏叆excel @@ -42,7 +54,12 @@ //鍒涘缓涓�涓伐浣滅翱 XSSFWorkbook workbook = new XSSFWorkbook(); //鍒涘缓sheet1 - createSheet1(workbook,battGroupId,testRecordCount); + //createSheet1(workbook,battGroupId,testRecordCount); + //鍒涘缓sheet2 -- 姣忔湀鐢靛帇,12涓湀,鍙栨瘡鏈�1鍙烽浂鐐归浂鍒� + //鍒涘缓sheet3 -- 姣忔湀鍐呴樆, 12涓湀,鍙栨瘡鏈�1鍙烽浂鐐归浂鍒� + createSheet2And3(workbook,battGroupId); + //鍒涘缓sheet4 -- 姣忓ぉ娓╁害, 30澶�, 鍙栨瘡澶╅浂鐐归浂鍒� + createSheet4(workbook,battGroupId); // 灏嗗伐浣滅翱鍐欏叆鏂囦欢 FileOutputStream fileOut = new FileOutputStream(filePath); @@ -51,6 +68,379 @@ // 鍏抽棴宸ヤ綔绨� workbook.close(); + } + + private void createSheet2And3(XSSFWorkbook workbook, int battGroupId) { + XSSFSheet sheet2 = workbook.createSheet("姣忔湀鐢靛帇"); + XSSFSheet sheet3 = workbook.createSheet("姣忔湀鍐呴樆"); + //鏁版嵁鍑嗗 + PowerInf battInfo = powerInfService.getByBattGroupId(battGroupId); + //鍒涘缓绗竴琛� + XSSFCell cellOfSheet2 = sheet2.createRow(0).createCell(0); + XSSFCell cellOfSheet3 = sheet3.createRow(0).createCell(0); + cellOfSheet2.setCellValue( battInfo.getPowerName()+ battInfo.getBattGroupName()+"娴厖鐢靛帇"); + cellOfSheet3.setCellValue( battInfo.getPowerName()+ battInfo.getBattGroupName()+"鍐呴樆鏁版嵁"); + + int columnCount = 2; + //鏌ヨ鍘嗗彶瀹炴椂琛�. 瀛樺湪鐨勬湀浠�. 濡傛灉瀛樺湪鐨勮瘽,鍙栫涓�绗旇褰�(104鏉�) + //棣栧厛鏌ヨ褰撳墠鏈堜唤瀵瑰簲鐨勬棩鏈� + List<String> tableListLike = commonMapper.getTableListLike("db_power_history", "tb_batt_realdata_"+battGroupId); + //褰撳墠鏈堜唤寰�鍓嶆帹11涓湀 + List<String> tableListInDB = new ArrayList<>(); + Calendar calendar = Calendar.getInstance(); + for (int i = 0; i < 12; i++) { + int year = calendar.get(Calendar.YEAR); + int month = calendar.get(Calendar.MONTH) + 1; + String monthStr; + if (month < 10) { + monthStr = "0" + month; + } else { + monthStr = String.valueOf(month); + } + if (tableListLike.contains("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr)) { + //瀛樺湪,鍒楁暟+1 + columnCount++; + tableListInDB.add("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr); + } + calendar.add(Calendar.MONTH, -1); + } + + //鍒楁牴鎹疄闄呮儏鍐靛彉鍔� + sheet2.addMergedRegion(new CellRangeAddress(0, 0, 0, columnCount-1)); + sheet3.addMergedRegion(new CellRangeAddress(0, 0, 0, columnCount-1)); + //鍒楀 + for (int j = 0; j < columnCount; j++ ) { + if(j==0){ + sheet2.setColumnWidth(j, 170 * 20); + sheet3.setColumnWidth(j, 170 * 20); + }else { + sheet2.setColumnWidth(j, 128 * 20); + sheet3.setColumnWidth(j, 128 * 20); + } + } + //琛屽 + sheet2.getRow(0).setHeightInPoints(40); + sheet3.getRow(0).setHeightInPoints(40); + //List鍏冪礌鍗囧簭鎺掑簭 + tableListInDB.sort(Comparator.comparing(String::toString)); + + //鍒涘缓绗簩琛� + XSSFRow row1OfSheet2 = sheet2.createRow(1); + XSSFRow row1OfSheet3 = sheet3.createRow(1); + row1OfSheet2.createCell(0).setCellValue("钃勭數姹犲彿锛�#锛�"); + row1OfSheet3.createCell(0).setCellValue("钃勭數姹犲彿锛�#锛�"); + //鎬诲垪鏁�-1 + for (int i = 0; i < columnCount - 1; i++) { + if(i == columnCount-2){ + row1OfSheet2.createCell(i+1).setCellValue("澶囨敞"); + row1OfSheet3.createCell(i+1).setCellValue("澶囨敞"); + }else { + row1OfSheet2.createCell(i + 1).setCellValue("璁板綍" + (i + 1)); + row1OfSheet3.createCell(i + 1).setCellValue("璁板綍" + (i + 1)); + } + } + + //濉厖鏁版嵁 + for (int i = 1; i <= 109; i++) { + if(i<=104) { + sheet2.createRow(i + 2).createCell(0).setCellValue(i + "#"); + sheet3.createRow(i + 2).createCell(0).setCellValue(i + "#"); + }else{ + if(i == 105){ + sheet2.createRow(i + 2).createCell(0).setCellValue("鍗曡妭骞冲潎鐢靛帇"); + sheet3.createRow(i + 2).createCell(0).setCellValue("鍗曡妭骞冲潎鍐呴樆"); + }else if (i == 106){ + sheet2.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶х數鍘�"); + sheet3.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶у唴闃�"); + } else if (i == 107) { + sheet2.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�灏忕數鍘�"); + sheet3.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�灏忓唴闃�"); + }else if (i == 108) { + sheet2.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶у樊鍊�"); + sheet3.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶у唴闃诲樊鍊�"); + }else { + sheet2.createRow(i + 2).createCell(0).setCellValue("鍧囧帇绯绘暟"); + } + } + } + //鍒涘缓绗笁琛� + XSSFRow row2OfSheet2 = sheet2.createRow(2); + XSSFRow row2OfSheet3 = sheet3.createRow(2); + + sheet2.getRow(0).createCell(columnCount-1); + sheet3.getRow(0).createCell(columnCount-1); + for (int i = 0; i <= tableListInDB.size(); i++) { //杩欎釜鏄垪鐨勯亶鍘� + if (i == tableListInDB.size()) { + //鏈�鍚庝竴鍒楀娉�:鍏ㄩ儴濉┖ + row2OfSheet2.createCell(i + 1); + row2OfSheet3.createCell(i + 1); + for (int j = 1; j <= 108; j++) { //杩欎釜鏄鐨勯亶鍘� + sheet2.getRow(j + 2).createCell(i + 1); + sheet3.getRow(j + 2).createCell(i + 1); + } + sheet2.getRow(108 + 2 + 1).createCell(i + 1); + } else { + //鑾峰彇鏁版嵁 + List<BattRealTimeDataHistory> list = battRTDataHisService.getFirstRecordList(tableListInDB.get(i)); + list.sort(Comparator.comparing(BattRealTimeDataHistory::getMonNum)); + //绗笁琛岀殑鏃堕棿鍒楀~鍏� + Date testStartTime = list.get(0).getTestStartTime(); + //鑾峰彇骞存湀 + String testMonth = DateUtil.YYYY_DOT_MM.format(testStartTime); + row2OfSheet2.createCell(i + 1).setCellValue(testMonth); + row2OfSheet3.createCell(i + 1).setCellValue(testMonth); + addStatistics(list, "vol", "res"); + for (int j = 1; j <= 108; j++) { //杩欎釜鏄鐨勯亶鍘� + sheet2.getRow(j + 2).createCell(i + 1).setCellValue((list.get(j - 1).getMonVol())); + sheet3.getRow(j + 2).createCell(i + 1).setCellValue((list.get(j - 1).getMonRes())); + } + Float volAvg = list.get(104).getMonVol(); + Float volMax = list.get(105).getMonVol(); + sheet2.getRow(108 + 2 + 1).createCell(i + 1).setCellValue(new BigDecimal(volAvg).divide(new BigDecimal(volMax), 4, RoundingMode.HALF_UP).floatValue()); + } + } + + addGlobalStylesToAllCells(sheet2, workbook); + addGlobalStylesToAllCells(sheet3, workbook); + + //璁剧疆sheet2鍜宻heet3鐨勬爣棰樻牱寮� + XSSFCellStyle cellStyleOriginal = cellOfSheet2.getCellStyle(); + XSSFCellStyle cellStyleNew = getCellStyleFont(cellStyleOriginal,workbook, true, 15); + cellOfSheet2.setCellStyle(cellStyleNew); + + XSSFCellStyle cellStyleOriginal2 = cellOfSheet3.getCellStyle(); + XSSFCellStyle cellStyleNew2 = getCellStyleFont(cellStyleOriginal2,workbook, true, 15); + cellOfSheet3.setCellStyle(cellStyleNew2); + + + // 鍒涘缓缁樺浘宸ュ叿 + createChart(sheet2, columnCount,"鐢靛帇鎶樼嚎鍥�"); + createChart(sheet3, columnCount,"鍐呴樆鎶樼嚎鍥�"); + + } + + private void createChart(XSSFSheet sheet2, int columnCount, String titleTextSuffix) { + XSSFDrawing drawing = sheet2.createDrawingPatriarch(); + //108涓崟浣�,10涓崟浣撲竴涓浘鏍� + int chartCol = 1; + + for (int i = 0; i < 104; i += 10) { + //XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 15, chartCol, 25, chartCol+12); + XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, columnCount+1, chartCol, columnCount+11, chartCol+12); + //鍥捐〃闂存瘡鍗佷簩鍒椾竴涓棿闅� + chartCol += 12; + String titleText; + if(i == 100){ + titleText = "101~104#钃勭數姹�"+titleTextSuffix; + }else{ + titleText = (i+1)+"~"+(i+10)+"#钃勭數姹�"+titleTextSuffix; + } + // 鍒涘缓鍥捐〃 + XSSFChart chart = drawing.createChart(anchor); + //chart.setTitleText(titleText); + CTChart ctChart = chart.getCTChart(); + CTTitle title = ctChart.getTitle(); + if (title == null) { + title = ctChart.addNewTitle(); + } + // 鍒涘缓鏂囨湰浣擄紙濡傛灉涓嶅瓨鍦級 + CTTx tx = title.addNewTx(); + CTTextBody txBody = tx.isSetStrRef() ? null : tx.addNewRich(); + + if (txBody == null) { + txBody = tx.getRich(); + } + + // 娓呯┖宸叉湁娈佃惤 + txBody.setPArray(new CTTextParagraph[0]); + + // 娣诲姞鏂扮殑娈佃惤鍜屾枃鏈繍琛� + CTTextParagraph p = txBody.addNewP(); + CTRegularTextRun r = p.addNewR(); + r.setT(titleText); + + // 璁剧疆瀛椾綋澶у皬锛�14pt -> 1400锛� + CTTextCharacterProperties rPr = r.addNewRPr(); + rPr.setSz(900); // 瀛楀彿锛�14pt + // 璁剧疆鍧愭爣杞� + XDDFCategoryAxis categoryAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); + //categoryAxis.setTitle("鐢垫睜缂栧彿"); + CTCatAx ctCatAx = getCTCatAx(categoryAxis, chart); + if(ctCatAx.isSetTitle()){ + ctCatAx.unsetTitle(); + } + + XDDFValueAxis valueAxis = chart.createValueAxis(AxisPosition.LEFT); + valueAxis.setCrosses(AxisCrosses.AUTO_ZERO); + CTValAx ctValAx = getCTValAx(valueAxis, chart); + if(ctValAx.isSetTitle()){ + ctValAx.unsetTitle(); + } + + //澧炲姞 缃戞牸绾垮拰鍥句緥 + XDDFShapeProperties gridProperties = valueAxis.getOrAddMajorGridProperties(); + XDDFLineProperties lineProperties = new XDDFLineProperties(); + lineProperties.setCompoundLine(CompoundLine.SINGLE); + lineProperties.setWidth(0.5); + gridProperties.setLineProperties(lineProperties); + + chart.getOrAddLegend().setPosition(LegendPosition.TOP_RIGHT); + chart.setTitleOverlay(true); + + // X杞存暟鎹簮-璁板綍缂栧彿 + //XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet2, new CellRangeAddress(2, 2, 1, 12)); + XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet2, new CellRangeAddress(2, 2, 1, columnCount-2)); + // 姣忎釜鐢垫睜鍗曚綋浣滀负涓�涓郴鍒�,瀵瑰簲涓�鏉℃姌绾� + // 鍒涘缓鎶樼嚎鍥剧郴鍒� + XDDFLineChartData dataChart = (XDDFLineChartData) chart.createData(ChartTypes.LINE, categoryAxis, valueAxis); + for (int j = i ; j < i+10; j++) { + if(j<104) { + XDDFNumericalDataSource<Double> ys = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(j + 3, j + 3, 1, columnCount -2)); + XDDFLineChartData.Series series = (XDDFLineChartData.Series) dataChart.addSeries(xs, ys); + //閫夋嫨甯冨眬1 + series.setTitle(((XSSFRow) sheet2.getRow(j + 3)).getCell(0).getStringCellValue(), new CellReference(sheet2.getSheetName(), j + 3, 0, true, true)); + series.setMarkerStyle(MarkerStyle.CIRCLE); // 璁剧疆鏍囪鐐� + } + + } + + // 灏嗘暟鎹~鍏呭埌鍥捐〃涓� + chart.plot(dataChart); + } + } + + private void createSheet4(XSSFWorkbook workbook, int battGroupId) { + XSSFSheet sheet4 = workbook.createSheet("姣忓ぉ娓╁害"); + //鏁版嵁鍑嗗 + PowerInf battInfo = powerInfService.getByBattGroupId(battGroupId); + //鍒涘缓绗竴琛� + XSSFCell cellOfSheet4 = sheet4.createRow(0).createCell(0); + cellOfSheet4.setCellValue( battInfo.getPowerName()+ battInfo.getBattGroupName()+"娓╁害鏁版嵁"); + + int columnCount = 2; + //鏌ヨ鍘嗗彶瀹炴椂琛�. 瀛樺湪鐨勬湀浠�. 濡傛灉瀛樺湪鐨勮瘽,鍙栫涓�绗旇褰�(104鏉�) + //棣栧厛鏌ヨ褰撳墠鏈堜唤瀵瑰簲鐨勬棩鏈� + List<String> tableListLike = commonMapper.getTableListLike("db_power_history", "tb_batt_realdata_"+battGroupId); + //褰撳墠鏈堜唤寰�鍓嶆帹11涓湀 + List<String> tableListInDB = new ArrayList<>(); + Calendar calendar = Calendar.getInstance(); + for (int i = 0; i < 12; i++) { + int year = calendar.get(Calendar.YEAR); + int month = calendar.get(Calendar.MONTH) + 1; + String monthStr; + if (month < 10) { + monthStr = "0" + month; + } else { + monthStr = String.valueOf(month); + } + if (tableListLike.contains("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr)) { + //瀛樺湪,鍒楁暟+1 + columnCount++; + tableListInDB.add("tb_batt_realdata_" + battGroupId + "_" + year + "_" + monthStr); + } + calendar.add(Calendar.MONTH, -1); + } + + //鍒楁牴鎹疄闄呮儏鍐靛彉鍔� + sheet4.addMergedRegion(new CellRangeAddress(0, 0, 0, columnCount-1)); + //鍒楀 + for (int j = 0; j < columnCount; j++ ) { + if(j==0){ + sheet4.setColumnWidth(j, 170 * 20); + }else { + sheet4.setColumnWidth(j, 128 * 20); + } + } + //琛屽 + sheet4.getRow(0).setHeightInPoints(40); + //List鍏冪礌鍗囧簭鎺掑簭 + tableListInDB.sort(Comparator.comparing(String::toString)); + + //鍒涘缓绗簩琛� + XSSFRow row1OfSheet4 = sheet4.createRow(1); + row1OfSheet4.createCell(0).setCellValue("钃勭數姹犲彿锛�#锛�"); + //鎬诲垪鏁�-1 + for (int i = 0; i < columnCount - 1; i++) { + if(i == columnCount-2){ + row1OfSheet4.createCell(i+1).setCellValue("澶囨敞"); + }else { + row1OfSheet4.createCell(i + 1).setCellValue("璁板綍" + (i + 1)); + } + } + + //濉厖鏁版嵁 + for (int i = 1; i <= 109; i++) { + if(i<=104) { + sheet4.createRow(i + 2).createCell(0).setCellValue(i + "#"); + }else{ + if(i == 105){ + sheet4.createRow(i + 2).createCell(0).setCellValue("鍗曡妭骞冲潎娓╁害"); + }else if (i == 106){ + sheet4.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶ф俯搴�"); + } else if (i == 107) { + sheet4.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�灏忔俯搴�"); + }else if (i == 108) { + sheet4.createRow(i + 2).createCell(0).setCellValue("鍗曡妭鏈�澶ф俯搴�"); + } + } + } + //鍒涘缓绗笁琛� + XSSFRow row2OfSheet4 = sheet4.createRow(2); + + sheet4.getRow(0).createCell(columnCount-1); + for (int i = 0; i <= tableListInDB.size(); i++) { //杩欎釜鏄垪鐨勯亶鍘� + if (i == tableListInDB.size()) { + //鏈�鍚庝竴鍒楀娉�:鍏ㄩ儴濉┖ + row2OfSheet4.createCell(i + 1); + for (int j = 1; j <= 108; j++) { //杩欎釜鏄鐨勯亶鍘� + sheet4.getRow(j + 2).createCell(i + 1); + } + } else { + //鑾峰彇鏁版嵁 + List<BattRealTimeDataHistory> list = battRTDataHisService.getFirstRecordList(tableListInDB.get(i)); + list.sort(Comparator.comparing(BattRealTimeDataHistory::getMonNum)); + //绗笁琛岀殑鏃堕棿鍒楀~鍏� + Date testStartTime = list.get(0).getTestStartTime(); + //鑾峰彇骞存湀 + String testMonth = DateUtil.YYYY_DOT_MM.format(testStartTime); + row2OfSheet4.createCell(i + 1).setCellValue(testMonth); + addStatistics(list, "vol", "res"); + for (int j = 1; j <= 108; j++) { //杩欎釜鏄鐨勯亶鍘� + sheet4.getRow(j + 2).createCell(i + 1).setCellValue((list.get(j - 1).getMonVol())); + } + } + } + + addGlobalStylesToAllCells(sheet4, workbook); + + //璁剧疆sheet4鍜宻heet3鐨勬爣棰樻牱寮� + XSSFCellStyle cellStyleOriginal = cellOfSheet4.getCellStyle(); + XSSFCellStyle cellStyleNew = getCellStyleFont(cellStyleOriginal,workbook, true, 15); + cellOfSheet4.setCellStyle(cellStyleNew); + + createChart(sheet4, columnCount,"娓╁害鎶樼嚎鍥�"); + + } + public CTValAx getCTValAx(XDDFValueAxis valueAxis, XSSFChart chart) { + // 浣跨敤鍙嶅皠鑾峰彇绉佹湁瀛楁 + try { + java.lang.reflect.Field field = valueAxis.getClass().getDeclaredField("ctValAx"); + field.setAccessible(true); + return (CTValAx) field.get(valueAxis); + } catch (Exception e) { + throw new RuntimeException("鏃犳硶鑾峰彇 CTValAx 瀹炰緥", e); + } + } + + public CTCatAx getCTCatAx(XDDFCategoryAxis categoryAxis, XSSFChart chart) { + // 浣跨敤鍙嶅皠鑾峰彇绉佹湁瀛楁 + try { + java.lang.reflect.Field field = categoryAxis.getClass().getDeclaredField("ctCatAx"); + field.setAccessible(true); + return (CTCatAx) field.get(categoryAxis); + } catch (Exception e) { + throw new RuntimeException("鏃犳硶鑾峰彇 CTCatAx 瀹炰緥", e); + } } public static void main(String[] args) throws IOException { @@ -124,15 +514,11 @@ //鍒楁暟鐨勮绠�= 钃勭數姹犲彿(1琛�)+娴厖鐢靛帇鍊�(2琛�)+鏀剧數鍓嶅紑璺數鍘�(2琛�)+鏀剧數鐗瑰畾鏃堕棿鐢靛帇(dischargeColumnCount琛�)+鏀剧數缁撴潫0.5h寮�璺數鍘嬪��(1琛�)+鍏呯數鐗瑰畾鏃堕棿鐢靛帇(chargeColumnCount琛�)+婊$數寮�璺數鍘嬪��(1琛�)+澶囨敞(1琛�) int columnCount = 1 + 2 + 2 + dischargeColumnCount + 1 + chargeColumnCount + 1 + 1; - //瀛椾綋鍔犵矖鏍峰紡 - XSSFCellStyle cellStyleFontBold = workbook.createCellStyle(); - Font font = workbook.createFont(); - font.setBold(true); - cellStyleFontBold.setFont(font); + //XSSFCellStyle cellStyleFontBold = getCellStyleFont(cellStyleOriginal, workbook,true,20); //鍒涘缓绗竴琛屾槸鏍囬琛� XSSFCell cellRow1 = sheet.createRow(0).createCell(0); - setRowStyle(sheet.getRow(0),cellStyleFontBold); + //setRowStyle(sheet.getRow(0),cellStyleFontBold); cellRow1.setCellValue(battInfo.getPowerName()+battInfo.getBattGroupName()+"鏍稿鎬ф斁鐢点�佸厖鐢佃瘯楠岃褰曡〃"); //鍚堝苟琛屽垪,鍗犳嵁鐨勫垪鏄痗olumnCount鍒楀悎骞朵负1鍒楀苟灞呬腑 @@ -199,7 +585,7 @@ cell6Row2.setCellValue("鐜娓╁害锛堚剝锛夛細24.5\n" + "鐜婀垮害锛�%锛夛細46.7"); //绗簩琛岃缃姞绮� - setRowStyle(sheet.getRow(1),cellStyleFontBold); + //setRowStyle(sheet.getRow(1),cellStyleFontBold); //绗笁琛屾槸鐢垫睜鍙峰拰鐢靛帇绛夌浉鍏虫姮澶� XSSFRow row2 = sheet.createRow(2); @@ -258,8 +644,8 @@ List<BattRealTimeDataHistory> preVolList = new ArrayList<>(); preVolList = battRTDataHisService.getPreVolList(battGroupId,dischargeStartTime); - addStatistics(fcVolList); - addStatistics(preVolList); + addStatistics(fcVolList,"vol"); + addStatistics(preVolList,"vol"); //涓�鍏�104鑺傚崟浣�,鍐嶅姞涓婄粺璁℃暟鎹�4鍒�,涓�鍏�108 for(int j = 0; j < 108; j++){ @@ -299,7 +685,7 @@ List<BattRealTimeDataHistory> dischargeListOne = battRTDataHisService.getRecordList(battGroupId,recordTime); //鎸夊崟浣撶紪鍙锋帓搴�,鎸夌紪鍙峰崌搴� dischargeListOne.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(dischargeListOne); + addStatistics(dischargeListOne,"vol"); //rowTemp.createCell(5).setCellValue("鏀剧數0.5h鐢靛帇鍊硷紙V锛�:"+(j+1)+"#"); //rowTemp.createCell(6).setCellValue("鏀剧數0.5h娓╁害鍊�:"+(j+1)+"#"); rowTemp.createCell(5).setCellValue(dischargeListOne.get(j).getMonVol()); @@ -312,7 +698,7 @@ Date recordTimeTwo = dischargeCalendar.getTime(); List<BattRealTimeDataHistory> dischargeListTwo = battRTDataHisService.getRecordList(battGroupId,recordTimeTwo); dischargeListTwo.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(dischargeListTwo); + addStatistics(dischargeListTwo,"vol"); //rowTemp.createCell(7).setCellValue("鏀剧數1h鐢靛帇鍊硷紙V锛�:"+(j+1)+"#"); //rowTemp.createCell(8).setCellValue("鏀剧數1h娓╁害鍊�:"+(j+1)+"#"); rowTemp.createCell(7).setCellValue(dischargeListTwo.get(j).getMonVol()); @@ -329,7 +715,7 @@ Date recordTimeN = dischargeCalendar.getTime(); List<BattRealTimeDataHistory> dischargeListN = battRTDataHisService.getRecordList(battGroupId,recordTimeN); dischargeListN.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(dischargeListN); + addStatistics(dischargeListN,"vol"); //rowTemp.createCell(i+5).setCellValue("鏀剧數" + dischargeHour + "h钃勭數姹犵數鍘嬪�硷紙V锛�"); rowTemp.createCell(i+5).setCellValue(dischargeListN.get(j).getMonVol()); columnIndexTemp++; @@ -345,7 +731,7 @@ dischargeEndCalendar.add(Calendar.MINUTE,30); List<BattRealTimeDataHistory> dischargeSetVolListOne = battRTDataHisService.getRecordList(battGroupId, dischargeEndCalendar.getTime()); dischargeSetVolListOne.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(dischargeSetVolListOne); + addStatistics(dischargeSetVolListOne,"vol"); rowTemp.createCell(++columnIndexTemp).setCellValue(dischargeSetVolListOne.get(j).getMonVol()); //瀹氫綅鍒板厖鐢靛紑濮嬫椂闂�,鐘舵�佸彉涓哄厖鐢电殑璁板綍鏃堕棿 @@ -356,7 +742,7 @@ chargeCalendar.add(Calendar.MINUTE,30); List<BattRealTimeDataHistory> chargeVolListOne = battRTDataHisService.getRecordList(battGroupId, chargeCalendar.getTime()); chargeVolListOne.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(chargeVolListOne); + addStatistics(chargeVolListOne,"vol"); columnIndexTemp++; //rowTemp.createCell(columnIndexTemp).setCellValue("鍏呯數0.5h"); rowTemp.createCell(columnIndexTemp).setCellValue(chargeVolListOne.get(j).getMonVol()); @@ -370,7 +756,7 @@ chargeCalendar.add(Calendar.HOUR,i); List<BattRealTimeDataHistory> chargeVolListN = battRTDataHisService.getRecordList(battGroupId, chargeCalendar.getTime()); chargeVolListN.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(chargeVolListN); + addStatistics(chargeVolListN,"vol"); //rowTemp.createCell(columnIndexTemp).setCellValue("鍏呯數" + (i) + "h钃勭數姹犵數鍘嬪�硷紙V锛�"); rowTemp.createCell(columnIndexTemp).setCellValue(chargeVolListN.get(j).getMonVol()); } @@ -382,7 +768,7 @@ chargeCalendar.setTime(chargeStartTime); List<BattRealTimeDataHistory> fcVolListAfter = battRTDataHisService.getFcVolListAfter(battGroupId, chargeCalendar.getTime()); fcVolListAfter.sort(Comparator.comparingInt(BattRealTimeDataHistory::getMonNum)); - addStatistics(fcVolListAfter); + addStatistics(fcVolListAfter,"vol"); //rowTemp.createCell(++columnIndexTemp).setCellValue("鍏呮弧鍚庤搫鐢垫睜鐢靛帇鍊硷紙V)锛堢數娴佷负0锛�"); if (fcVolListAfter.size() > 0){ rowTemp.createCell(++columnIndexTemp).setCellValue(fcVolListAfter.get(j).getMonVol()); @@ -399,41 +785,116 @@ } //瀵规暣涓〃鏍艰繘琛屽叏灞�璁剧疆,鏆備负璁剧疆杈规 addGlobalStylesToAllCells(sheet, workbook); + + //鍒涘缓绗竴琛屾槸鏍囬琛� + setRowStyle(workbook,sheet.getRow(0),true,15); + //绗簩琛岃缃姞绮� + setRowStyle(workbook,sheet.getRow(1),true,15); } - //瀵逛紶鍏ョ殑BattRealTimeDataHistory鍒楄〃杩涜缁熻,灏嗙粺璁$粨鏋滀篃杩藉姞鍒板垪琛ㄤ腑 - public void addStatistics(List<BattRealTimeDataHistory> battRealTimeDataHistoryList) { + private XSSFCellStyle getCellStyleFont(XSSFCellStyle cellStyleOriginal, XSSFWorkbook workbook, boolean isFontBold, int fontSize) { + //瀛椾綋鍔犵矖鏍峰紡 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) (fontSize)); + font.setBold(isFontBold); + + XSSFCellStyle cellStyle = workbook.createCellStyle(); + cellStyle.cloneStyleFrom(cellStyleOriginal); + cellStyle.setFont(font); + cellStyle.setAlignment(HorizontalAlignment.CENTER); + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + return cellStyle; + } + + //瀵逛紶鍏ョ殑BattRealTimeDataHistory鍒楄〃杩涜缁熻,灏嗙粺璁$粨鏋滀篃杩藉姞鍒板垪琛ㄤ腑. 缁熻鐨勪负鐢靛帇 + public void addStatistics(List<BattRealTimeDataHistory> battRealTimeDataHistoryList,String... fields) { + List<String> fieldList = Arrays.asList(fields); if(battRealTimeDataHistoryList != null && battRealTimeDataHistoryList.size() > 0){ - //鐢靛帇骞冲潎鍊�,鏈�澶у��,鏈�灏忓��,鏈�澶у樊鍊� - List<Float> volList = battRealTimeDataHistoryList.stream().map(BattRealTimeDataHistory::getMonVol).collect(Collectors.toList()); - Double volAvgDouble = volList.stream().collect(Collectors.averagingDouble(Float::floatValue)); - BigDecimal avg = new BigDecimal(volAvgDouble).setScale(4, RoundingMode.HALF_UP); - float volAvg = avg.floatValue(); - - //鏈�澶у�� - float volMax = volList.stream().max(Float::compareTo).get(); - BigDecimal max = new BigDecimal(volMax).setScale(4, RoundingMode.HALF_UP); - volMax = max.floatValue(); - - //鏈�灏忓�� - float volMin = volList.stream().min(Float::compareTo).get(); - BigDecimal min = new BigDecimal(volMin).setScale(4, RoundingMode.HALF_UP); - volMin = min.floatValue(); - - //鏈�澶у樊鍊� - float volGap = volMax - volMin; - BattRealTimeDataHistory hisAvg = new BattRealTimeDataHistory(); - hisAvg.setMonVol(volAvg); - BattRealTimeDataHistory hisMax = new BattRealTimeDataHistory(); - hisMax.setMonVol(volMax); - BattRealTimeDataHistory hisMin = new BattRealTimeDataHistory(); - hisMin.setMonVol(volMin); - BattRealTimeDataHistory hisGap = new BattRealTimeDataHistory(); - hisGap.setMonVol(volGap); + if(fieldList.contains("vol")) { + //鐢靛帇骞冲潎鍊�,鏈�澶у��,鏈�灏忓��,鏈�澶у樊鍊� + List<Float> volList = battRealTimeDataHistoryList.stream().map(BattRealTimeDataHistory::getMonVol).collect(Collectors.toList()); + Double volAvgDouble = volList.stream().collect(Collectors.averagingDouble(Float::floatValue)); + BigDecimal avg = new BigDecimal(volAvgDouble).setScale(4, RoundingMode.HALF_UP); + float volAvg = avg.floatValue(); + + //鏈�澶у�� + float volMax = volList.stream().max(Float::compareTo).get(); + BigDecimal max = new BigDecimal(volMax).setScale(4, RoundingMode.HALF_UP); + volMax = max.floatValue(); + + //鏈�灏忓�� + float volMin = volList.stream().min(Float::compareTo).get(); + BigDecimal min = new BigDecimal(volMin).setScale(4, RoundingMode.HALF_UP); + volMin = min.floatValue(); + + //鏈�澶у樊鍊� + float volGap = volMax - volMin; + + hisAvg.setMonVol(volAvg); + + hisMax.setMonVol(volMax); + + hisMin.setMonVol(volMin); + + hisGap.setMonVol(volGap); + } + + if(fieldList.contains("res")) { + //骞冲潎鍊�,鏈�澶у��,鏈�灏忓��,鏈�澶у樊鍊� + List<Float> resList = battRealTimeDataHistoryList.stream().map(BattRealTimeDataHistory::getMonRes).collect(Collectors.toList()); + Double resAvgDouble = resList.stream().collect(Collectors.averagingDouble(Float::floatValue)); + BigDecimal avg = new BigDecimal(resAvgDouble).setScale(4, RoundingMode.HALF_UP); + float resAvg = avg.floatValue(); + + //鏈�澶у�� + float resMax = resList.stream().max(Float::compareTo).get(); + BigDecimal max = new BigDecimal(resMax).setScale(4, RoundingMode.HALF_UP); + resMax = max.floatValue(); + + //鏈�灏忓�� + float resMin = resList.stream().min(Float::compareTo).get(); + BigDecimal min = new BigDecimal(resMin).setScale(4, RoundingMode.HALF_UP); + resMin = min.floatValue(); + + //鏈�澶у樊鍊� + float resGap = resMax - resMin; + + hisAvg.setMonRes(resAvg); + hisMax.setMonRes(resMax); + hisMin.setMonRes(resMin); + hisGap.setMonRes(resGap); + } + + if(fieldList.contains("tmp")) { + //骞冲潎鍊�,鏈�澶у��,鏈�灏忓��,鏈�澶у樊鍊� + List<Float> tmpList = battRealTimeDataHistoryList.stream().map(BattRealTimeDataHistory::getMonTmp).collect(Collectors.toList()); + Double tmpAvgDouble = tmpList.stream().collect(Collectors.averagingDouble(Float::floatValue)); + BigDecimal avg = new BigDecimal(tmpAvgDouble).setScale(4, RoundingMode.HALF_UP); + float tmpAvg = avg.floatValue(); + + //鏈�澶у�� + float tmpMax = tmpList.stream().max(Float::compareTo).get(); + BigDecimal max = new BigDecimal(tmpMax).setScale(4, RoundingMode.HALF_UP); + tmpMax = max.floatValue(); + + //鏈�灏忓�� + float tmpMin = tmpList.stream().min(Float::compareTo).get(); + BigDecimal min = new BigDecimal(tmpMin).setScale(4, RoundingMode.HALF_UP); + tmpMin = min.floatValue(); + + //鏈�澶у樊鍊� + float tmpGap = tmpMax - tmpMin; + + hisAvg.setMonTmp(tmpAvg); + hisMax.setMonTmp(tmpMax); + hisMin.setMonTmp(tmpMin); + hisGap.setMonTmp(tmpGap); + } + battRealTimeDataHistoryList.add(hisAvg); battRealTimeDataHistoryList.add(hisMax); @@ -442,9 +903,19 @@ } } - public void setRowStyle(Row row, CellStyle style) { + public void setRowStyle(Workbook workbook,Row row,boolean isFontBold,int fontSize) { for (Cell cell : row) { - cell.setCellStyle(style); + //鍏堝厠闅嗗師鏉ョ殑灞炴�� + CellStyle cellStyleNew = workbook.createCellStyle(); + cellStyleNew.cloneStyleFrom(cell.getCellStyle()); + + //璁剧疆瀛椾綋鍜屽姞绮� + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) (fontSize)); + font.setBold(isFontBold); + + cellStyleNew.setFont(font); + cell.setCellStyle(cellStyleNew); } } -- Gitblit v1.9.1