From 44351a80214e1c37810722f1cfaa48cb4eda22ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=81=A5?= Date: Tue, 11 May 2021 10:22:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A1=B9=E7=9B=AE=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E5=8D=95=E5=85=83=E6=A0=BC=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shenjian/statistics/ProjectExport.groovy | 10 ++++- .../com/fangjia/script/util/ExcelUtils.groovy | 37 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/statistics/ProjectExport.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/statistics/ProjectExport.groovy index 76ea84abe..d08846701 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/statistics/ProjectExport.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/statistics/ProjectExport.groovy @@ -18,6 +18,7 @@ class ProjectExport { static final String endTime = "2021-04-30" static void main(String[] args) { + List> mergeData = new ArrayList<>() def sort = ["建行项目", "基础建设", "非农行项目", "农行项目"]; def result = [] @@ -57,14 +58,21 @@ class ProjectExport { def sortedResult = [] sortedResult << ['项目类别', '项目名称', '项目负责人', '总工时(天)'] for (def label : sort) { + List mergeRowCel = new ArrayList<>() + mergeRowCel.add(sortedResult.size()) for (def ret : result) { if (ret[0] == label) { sortedResult << ret } } + mergeRowCel.add(sortedResult.size() - 1) + mergeRowCel.add(0) + mergeRowCel.add(0) + + mergeData.add(mergeRowCel) } - com.fangjia.script.util.ExcelUtils.write("F:\\数据\\绩效\\项目统计.xlsx", sortedResult, "项目统计"); + com.fangjia.script.util.ExcelUtils.write("F:\\数据\\绩效\\项目统计.xlsx", sortedResult, "项目统计", mergeData); } } diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ExcelUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ExcelUtils.groovy index 4cad85c53..203508a51 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ExcelUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/util/ExcelUtils.groovy @@ -1,11 +1,14 @@ package com.fangjia.script.util +import org.apache.commons.collections.CollectionUtils import org.apache.commons.lang.StringUtils import org.apache.poi.hssf.record.BoolErrRecord - +import org.apache.poi.hssf.usermodel.HSSFBorderFormatting import org.apache.poi.hssf.usermodel.HSSFWorkbook import org.apache.poi.ss.formula.eval.ErrorEval import org.apache.poi.ss.usermodel.* +import org.apache.poi.ss.util.CellRangeAddress +import org.apache.poi.ss.util.RegionUtil import org.apache.poi.xssf.streaming.SXSSFWorkbook import java.util.concurrent.ExecutorService @@ -235,7 +238,7 @@ public class ExcelUtils{ return super.getProperty(propertyName) } - public static void write(String fileName, List> data, String sheetName){ + public static void write(String fileName, List> data, String sheetName, List> mergeData){ File file = new File(fileName) File dir = file.getParentFile(); @@ -304,6 +307,8 @@ public class ExcelUtils{ throw e } } + mergeCell(workBook, mainSheet, mergeData) + for(int i = 0; i < maxColumnNum; i++ ){ mainSheet.autoSizeColumn(i) } @@ -321,6 +326,34 @@ public class ExcelUtils{ fos.close(); } + static void write(String fileName, List> data, String sheetName){ + write(fileName, data, sheetName, null) + } + + /** + * 合并单元格 + * @param workBook + * @param sheet + * @param data int firstRow, int lastRow, int firstCol, int lastCol + */ + static mergeCell(Workbook workBook, Sheet sheet, List> data) { + if (CollectionUtils.isEmpty(data)) { + return + } + //设置style + CellStyle mergedCellStyle = workBook.createCellStyle() + mergedCellStyle.verticalAlignment = VerticalAlignment.CENTER; + mergedCellStyle.alignment = HorizontalAlignment.CENTER; + + for (List rowCell : data) { + CellRangeAddress cra = new CellRangeAddress(rowCell.get(0), rowCell.get(1), rowCell.get(2), rowCell.get(3)) + sheet.addMergedRegion(cra); + //设置合并后style + def cell = sheet.getRow(rowCell.get(0)).getCell(rowCell.get(2)); + cell.setCellStyle(mergedCellStyle) + } + } + public static newWorkBook(String fileName){ Workbook wb; if(fileName.endsWith(".xlsx")){