新增项目统计单元格合并

This commit is contained in:
沈健
2021-05-11 10:22:53 +08:00
parent 66058f0a71
commit 44351a8021
2 changed files with 44 additions and 3 deletions
@@ -18,6 +18,7 @@ class ProjectExport {
static final String endTime = "2021-04-30"
static void main(String[] args) {
List<List<Integer>> mergeData = new ArrayList<>()
def sort = ["建行项目", "基础建设", "非农行项目", "农行项目"];
def result = []
@@ -57,14 +58,21 @@ class ProjectExport {
def sortedResult = []
sortedResult << ['项目类别', '项目名称', '项目负责人', '总工时(天)']
for (def label : sort) {
List<Integer> 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);
}
}
@@ -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<List<Object>> data, String sheetName){
public static void write(String fileName, List<List<Object>> data, String sheetName, List<List<Integer>> 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<List<Object>> 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<List<Integer>> data) {
if (CollectionUtils.isEmpty(data)) {
return
}
//设置style
CellStyle mergedCellStyle = workBook.createCellStyle()
mergedCellStyle.verticalAlignment = VerticalAlignment.CENTER;
mergedCellStyle.alignment = HorizontalAlignment.CENTER;
for (List<Integer> 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")){