From acc382b835b33e5133beb1173f522c40e8e42d24 Mon Sep 17 00:00:00 2001 From: sy Date: Wed, 15 Mar 2023 10:20:46 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=96=AA=E9=85=AC=E7=B3=BB=E7=BB=9F-?= =?UTF-8?q?=E8=96=AA=E8=B5=84=E6=A0=B8=E7=AE=97=EF=BC=8C=E6=A0=B8=E7=AE=97?= =?UTF-8?q?=E5=AF=BC=E5=87=BAexcel=E6=A0=BC=E5=BC=8F=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/SalaryAcctExcelServiceImpl.java | 95 +++-- .../salary/util/excel/ExcelUtilPlus.java | 333 ++++++++++++++++++ 2 files changed, 408 insertions(+), 20 deletions(-) create mode 100644 src/com/engine/salary/util/excel/ExcelUtilPlus.java diff --git a/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java b/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java index 732adbeb9..7a0fdb66d 100644 --- a/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java +++ b/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java @@ -39,6 +39,7 @@ import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.excel.ExcelParseHelper; import com.engine.salary.util.excel.ExcelSupport; import com.engine.salary.util.excel.ExcelUtil; +import com.engine.salary.util.excel.ExcelUtilPlus; import com.engine.salary.util.page.Column; import com.engine.salary.util.valid.ValidUtil; import com.google.common.collect.Lists; @@ -50,8 +51,11 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.ListUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import weaver.file.ImageFileManager; import weaver.hrm.User; @@ -276,11 +280,12 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc // 查询列表的表头 List weaTableColumns = listWeaTableColumn(salaryAcctRecordPO); parseHeader(headerColumnGroup, weaTableColumns); - // excel导出的表头 - String[] headers = headerColumnGroup.stream() - .map(WeaTableColumn::getText) - .toArray(String[]::new); - List headerList = new ArrayList<>(Arrays.asList(headers)); +// // excel导出的表头 +// String[] headers = headerColumnGroup.stream() +// .map(WeaTableColumn::getText) +// .toArray(String[]::new); +// List headerList = new ArrayList<>(Arrays.asList(headers)); + List headerList = new ArrayList<>(weaTableColumns); // 查询薪资核算结果 List> resultMapList = getSalaryAcctResultService(user).listByParam(queryParam); @@ -307,9 +312,11 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc } String sheetName = "薪资核算结果"; - return ExcelUtil.genWorkbookV2(rows, sheetName, total); +// return ExcelUtil.genWorkbookV2(rows, sheetName, total); + return ExcelUtilPlus.genWorkbookWithChildTitleColumn(rows, sheetName, total); } + /** * 构建薪资核算结果列表的表头 * @@ -408,38 +415,86 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc if (Objects.isNull(salaryAcctRecordPO)) { throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98747, "薪资核算记录不存在或已被删除")); } - // 模板表头(默认必带"个税扣缴义务人"、"姓名") - List headerList = Lists.newArrayList(SalaryI18nUtil.getI18nLabel(85429, "姓名"), - "部门", - SalaryI18nUtil.getI18nLabel(86186, "手机号"), - SalaryI18nUtil.getI18nLabel(86317, "工号"), - SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人")); - List dataIndexList = Lists.newArrayList("username", "departmentName", "mobile", "workcode", "taxAgentName"); +// // 模板表头(默认必带"个税扣缴义务人"、"姓名") +// List headerList = Lists.newArrayList(SalaryI18nUtil.getI18nLabel(85429, "姓名"), +// "部门", +// SalaryI18nUtil.getI18nLabel(86186, "手机号"), +// SalaryI18nUtil.getI18nLabel(86317, "工号"), +// SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人")); +// List dataIndexList = Lists.newArrayList("username", "departmentName", "mobile", "workcode", "taxAgentName"); +// // 查询薪资项目 +// List salaryItemPOS = getSalaryItemService(user).listByIds(param.getSalaryItemIds()); +// for (SalaryItemPO salaryItemPO : salaryItemPOS) { +// headerList.add(salaryItemPO.getName()); +// dataIndexList.add("" + salaryItemPO.getId()); +// } +// +// // 查询薪资核算结果 +// List> resultMapList = getSalaryAcctResultService(user).listByParam(param); +// // excel导出的数据 +// List> rows = Lists.newArrayListWithExpectedSize(resultMapList.size()); +// rows.add(headerList); +// for (Map map : resultMapList) { +// List row = Lists.newArrayListWithExpectedSize(headerList.size()); +// for (String dataIndex : dataIndexList) { +// row.add(map.getOrDefault(dataIndex, StringUtils.EMPTY)); +// } +// rows.add(row); +// } +// return ExcelUtil.genWorkbookV2(rows, sheetName); + // 查询薪资项目 + List headerRangeList = new ArrayList<>(); List salaryItemPOS = getSalaryItemService(user).listByIds(param.getSalaryItemIds()); for (SalaryItemPO salaryItemPO : salaryItemPOS) { - headerList.add(salaryItemPO.getName()); - dataIndexList.add("" + salaryItemPO.getId()); + headerRangeList.add(salaryItemPO.getId().toString()); } + headerRangeList.add("username"); + headerRangeList.add("mobile"); + headerRangeList.add("workcode"); + headerRangeList.add("taxAgentName"); + headerRangeList.add("departmentName"); + // 查询列表的表头 + List weaTableColumns = listWeaTableColumn(salaryAcctRecordPO); + List finalWeaTableColumns = new ArrayList<>(); + for (WeaTableColumnGroup tableColumn : weaTableColumns) { + WeaTableColumnGroup columnGroupItem = (WeaTableColumnGroup) tableColumn; + if (columnGroupItem.getChildren() != null) { + List childrenColumns = columnGroupItem.getChildren().stream().filter(f -> headerRangeList.contains(f.getColumn())).collect(Collectors.toList()); + if (childrenColumns.size() > 0) { + columnGroupItem.setChildren(childrenColumns); + finalWeaTableColumns.add(columnGroupItem); + } + } else if (headerRangeList.contains(columnGroupItem.getColumn())) { + finalWeaTableColumns.add(columnGroupItem); + } + } + // excel导出的表头 + List headerColumnGroup = Lists.newArrayList(); + // 查询列表的表头 + parseHeader(headerColumnGroup, finalWeaTableColumns); + + List headerList = new ArrayList<>(finalWeaTableColumns); // 查询薪资核算结果 List> resultMapList = getSalaryAcctResultService(user).listByParam(param); // excel导出的数据 - List> rows = Lists.newArrayListWithExpectedSize(resultMapList.size()); + List> rows = new ArrayList<>(); rows.add(headerList); for (Map map : resultMapList) { - List row = Lists.newArrayListWithExpectedSize(headerList.size()); - for (String dataIndex : dataIndexList) { - row.add(map.getOrDefault(dataIndex, StringUtils.EMPTY)); + List row = Lists.newArrayListWithExpectedSize(headerColumnGroup.size()); + for (WeaTableColumnGroup weaTableColumn : headerColumnGroup) { + row.add(map.getOrDefault(weaTableColumn.getColumn(), StringUtils.EMPTY)); } rows.add(row); } String sheetName = "薪资核算导入模板"; - return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookWithChildTitleColumn(rows, sheetName, false); } + @Override public XSSFWorkbook exportComparisonResult(SalaryComparisonResultQueryParam queryParam) { ValidUtil.doValidator(queryParam); diff --git a/src/com/engine/salary/util/excel/ExcelUtilPlus.java b/src/com/engine/salary/util/excel/ExcelUtilPlus.java new file mode 100644 index 000000000..b3b2768e5 --- /dev/null +++ b/src/com/engine/salary/util/excel/ExcelUtilPlus.java @@ -0,0 +1,333 @@ +package com.engine.salary.util.excel; + +import com.engine.salary.component.WeaTableColumnGroup; +import com.engine.salary.util.SalaryDateUtil; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.*; + +import java.awt.*; +import java.awt.Color; +import java.util.Date; +import java.util.List; + +public class ExcelUtilPlus { + /** + * 生成excel + * + * @param rowList + * @return + */ + public static XSSFWorkbook genWorkbook(List> rowList, String sheetName) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); + titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex); + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + cell.setCellType(CellType.STRING); + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + cell.setCellValue(infoList.get(cellIndex)); +// sheet.setColumnWidth(cellIndex, 35 * 256); + } + } + return workbook; + } + + + public static XSSFWorkbook genWorkbookV2(List> rowList, String sheetName) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); + titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex); + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + Object o = infoList.get(cellIndex); + if (o instanceof String) { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o)); + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(o == null ? "" : o.toString()); + } + } + } + return workbook; + } + + + public static XSSFWorkbook genWorkbookV2(List> rowList, String sheetName, List comments) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); + titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex); + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + Object o = infoList.get(cellIndex); + if (o instanceof String) { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o)); + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } + } + } + + if (CollectionUtils.isNotEmpty(comments)) { + for (ExcelComment c : comments) { + XSSFDrawing patr = sheet.createDrawingPatriarch(); + XSSFComment cellComment = patr.createCellComment(new XSSFClientAnchor(c.dx1, c.dy1, c.dx2, c.dy2, c.col1, c.row1, c.col2, c.row2)); + cellComment.setString(c.content); + } + } + return workbook; + } + + + public static XSSFWorkbook genWorkbookWithChildTitleColumn(List> rowList, String sheetName, boolean lastRowRed) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.LEFT); + titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleCellStyle.setBorderLeft(BorderStyle.THIN); + titleCellStyle.setBorderRight(BorderStyle.THIN); + titleCellStyle.setBorderTop(BorderStyle.THIN); + titleCellStyle.setBorderBottom(BorderStyle.THIN); + + + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFCellStyle redCellStyle = workbook.createCellStyle(); + XSSFFont redFont = workbook.createFont(); + redFont.setFontName("宋体"); + redFont.setFontHeightInPoints((short) 10);// 设置字体大小 + redFont.setColor(new XSSFColor(new Color(0xFF3333), null)); + redCellStyle.setWrapText(true); + redCellStyle.setFont(redFont);// 选择需要用到的字体格式 + + redCellStyle.setBorderLeft(BorderStyle.THIN); + redCellStyle.setBorderRight(BorderStyle.THIN); + redCellStyle.setBorderTop(BorderStyle.THIN); + redCellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + //处理合并单元格 + XSSFRow row0 = sheet.createRow(0); + XSSFRow row1 = sheet.createRow(1); + + List header = rowList.get(0); + int startIndex = 0; + for (int i = 0; i < header.size(); i++) { + WeaTableColumnGroup columnGroupItem = (WeaTableColumnGroup) header.get(i); + if (columnGroupItem.getChildren() == null) { + sheet.addMergedRegion(new CellRangeAddress(0, 1, startIndex, startIndex)); + + XSSFCell rowZeroCell = row0.createCell(startIndex, CellType.STRING); + rowZeroCell.setCellValue(columnGroupItem.getText().toString()); + rowZeroCell.setCellStyle(titleCellStyle); + XSSFCell rowOneCell = row1.createCell(startIndex, CellType.STRING); + rowOneCell.setCellStyle(titleCellStyle); + //设置列宽 + sheet.setColumnWidth(startIndex,columnGroupItem.getText().length()*4*256); + startIndex++; + } else { + List childrenList = columnGroupItem.getChildren(); + int endIndex = startIndex + childrenList.size() - 1; + + if (endIndex > startIndex) { + sheet.addMergedRegion(new CellRangeAddress(0, 0, startIndex, endIndex)); + } + + XSSFCell cell = row0.createCell(startIndex, CellType.STRING); + cell.setCellValue(columnGroupItem.getText().toString()); + cell.setCellStyle(titleCellStyle); + + for (int j = 0; j < childrenList.size(); j++) { + WeaTableColumnGroup childrenItem = (WeaTableColumnGroup) childrenList.get(j); + + XSSFCell subHeader = row1.createCell(startIndex + j, CellType.STRING); + subHeader.setCellValue(childrenItem.getText().toString()); + subHeader.setCellStyle(titleCellStyle); + //设置列宽 + sheet.setColumnWidth(startIndex + j,childrenItem.getText().length()*4*256); + } + + startIndex += childrenList.size(); + + } + + } + + for (int rowIndex = 1; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex + 1); + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + if (lastRowRed && rowIndex == rowList.size() - 1) { + cell.setCellStyle(redCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + } + Object o = infoList.get(cellIndex); + if (o instanceof String) { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o)); + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(o == null ? "" : o.toString()); + } + } + } + return workbook; + } +} From dc2cb5f44861fc65b1bf3eeb3eb0c90342d89c15 Mon Sep 17 00:00:00 2001 From: sy Date: Thu, 16 Mar 2023 13:51:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=96=AA=E9=85=AC=E7=B3=BB=E7=BB=9F-?= =?UTF-8?q?=E8=96=AA=E8=B5=84=E6=A0=B8=E7=AE=97=EF=BC=8C=E8=96=AA=E8=B5=84?= =?UTF-8?q?=E6=A0=B8=E7=AE=97=E5=92=8C=E8=96=AA=E8=B5=84=E6=A1=A3=E6=A1=88?= =?UTF-8?q?=E5=AF=BC=E5=87=BAexcel=E9=80=9A=E7=94=A8=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/SalaryAcctExcelService.java | 3 + .../impl/SalaryAcctExcelServiceImpl.java | 57 +++++++++++++++---- .../impl/SalaryArchiveExcelServiceImpl.java | 8 +-- .../impl/SalaryArchiveServiceImpl.java | 4 +- .../salary/util/excel/ExcelParseHelper.java | 46 +++++++++++++++ .../salary/util/excel/ExcelUtilPlus.java | 57 +++++++++++++++++-- .../salary/web/SalaryAcctController.java | 2 +- 7 files changed, 154 insertions(+), 23 deletions(-) diff --git a/src/com/engine/salary/service/SalaryAcctExcelService.java b/src/com/engine/salary/service/SalaryAcctExcelService.java index de731313b..444451125 100644 --- a/src/com/engine/salary/service/SalaryAcctExcelService.java +++ b/src/com/engine/salary/service/SalaryAcctExcelService.java @@ -105,6 +105,9 @@ public interface SalaryAcctExcelService { Map preview(SalaryAcctImportParam param); + Map previewImportSalaryAcctResult(SalaryAcctImportParam param); + + // // /** // * 薪资核算结果校验异常导出 diff --git a/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java b/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java index 7a0fdb66d..ad6d262ac 100644 --- a/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java +++ b/src/com/engine/salary/service/impl/SalaryAcctExcelServiceImpl.java @@ -166,8 +166,8 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc // 3.表数据 List> lists = convert2ExcelRow(salaryAcctEmployees); rows.addAll(lists); - return ExcelUtil.genWorkbookV2(rows, sheetName); - +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } @@ -199,8 +199,8 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc // 3.表数据 List> lists = convert2ExcelRow(salaryAcctEmployees); rows.addAll(lists); - return ExcelUtil.genWorkbookV2(rows, sheetName); - +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } @Override @@ -231,7 +231,8 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc // 3.表数据 List> lists = convert2ExcelRow(salaryAcctEmployees); rows.addAll(lists); - return ExcelUtil.genWorkbookV2(rows, sheetName); +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } private List> convert2ExcelRow(List salaryAcctEmployees) { @@ -549,7 +550,8 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc String sheetName = "线下对比结果"; - return ExcelUtil.genWorkbookV2(rows, sheetName); +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } @@ -597,7 +599,8 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc rows.add(headerList); String sheetName = "线下对比结果导入模板"; - return ExcelUtil.genWorkbookV2(rows, sheetName); +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } public Map importSalaryAcctResult(SalaryAcctImportParam param) { @@ -630,6 +633,27 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc } } + @Override + public Map previewImportSalaryAcctResult(SalaryAcctImportParam param) { + + //1、参数校验 + ValidUtil.doValidator(param); + + Map map = new HashMap<>(); + + InputStream fileInputStream = null; + try { + fileInputStream = ImageFileManager.getInputStreamById(Integer.parseInt(param.getImageId())); + Sheet sheet = ExcelSupport.parseFile(fileInputStream, 0, EXCEL_TYPE_XLSX); + map.put("headers", ExcelSupport.getSheetHeader(sheet, 1)); + map.put("list", ExcelParseHelper.parse2List(sheet, 2, 1)); + return map; + + } finally { + IOUtils.closeQuietly(fileInputStream); + } + } + private Map batchImport(SalaryAcctImportParam param, String importType) { Map apidatas = new HashMap(); ValidUtil.doValidator(param); @@ -703,10 +727,23 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc // 存在错误的那行数据 List> errorDatas = Lists.newArrayList(); // 表头 - List headers = ExcelSupport.getSheetHeader(sheet, 0); +// List headers = ExcelSupport.getSheetHeader(sheet, 0); + List headers; + if (StringUtils.equals("importSalaryAcctResult", importType)) { + headers = ExcelSupport.getSheetHeader(sheet, 1); + } else { + headers = ExcelSupport.getSheetHeader(sheet, 0); + } + + // 处理数值 +// List> data = ExcelParseHelper.parse2Map(sheet, 1); + List> data; + if (StringUtils.equals("importSalaryAcctResult", importType)) { + data = ExcelParseHelper.parse2Map(sheet, 2, 1); + } else { + data = ExcelParseHelper.parse2Map(sheet, 1); + } - // 处理数值 - List> data = ExcelParseHelper.parse2Map(sheet, 1); if (CollectionUtils.isEmpty(headers)) { throw new RuntimeException("表头为空"); } diff --git a/src/com/engine/salary/service/impl/SalaryArchiveExcelServiceImpl.java b/src/com/engine/salary/service/impl/SalaryArchiveExcelServiceImpl.java index 1065192e5..0c94107f6 100644 --- a/src/com/engine/salary/service/impl/SalaryArchiveExcelServiceImpl.java +++ b/src/com/engine/salary/service/impl/SalaryArchiveExcelServiceImpl.java @@ -27,10 +27,7 @@ import com.engine.salary.service.*; import com.engine.salary.util.SalaryEntityUtil; import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.db.MapperProxyFactory; -import com.engine.salary.util.excel.ExcelComment; -import com.engine.salary.util.excel.ExcelParseHelper; -import com.engine.salary.util.excel.ExcelSupport; -import com.engine.salary.util.excel.ExcelUtil; +import com.engine.salary.util.excel.*; import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -406,7 +403,8 @@ public class SalaryArchiveExcelServiceImpl extends Service implements SalaryArch // SalaryArchiveExcelBO.createExcelComment(excelComments, SalaryI18nUtil.getI18nLabel(109736, "格式样例为'2022-01-01'、'2022/1/1'"), 0, 0, i + 1, i + 1); } - return ExcelUtil.genWorkbookV2(rows, finalNameI18n, excelComments); +// return ExcelUtil.genWorkbookV2(rows, finalNameI18n, excelComments); + return ExcelUtilPlus.genWorkbookV2(rows, finalNameI18n, excelComments); } // /** diff --git a/src/com/engine/salary/service/impl/SalaryArchiveServiceImpl.java b/src/com/engine/salary/service/impl/SalaryArchiveServiceImpl.java index 68a5209b1..9a0874ee0 100644 --- a/src/com/engine/salary/service/impl/SalaryArchiveServiceImpl.java +++ b/src/com/engine/salary/service/impl/SalaryArchiveServiceImpl.java @@ -38,6 +38,7 @@ import com.engine.salary.util.db.MapperProxyFactory; import com.engine.salary.util.excel.ExcelParseHelper; import com.engine.salary.util.excel.ExcelSupport; import com.engine.salary.util.excel.ExcelUtil; +import com.engine.salary.util.excel.ExcelUtilPlus; import com.engine.salary.util.page.PageInfo; import com.engine.salary.util.page.SalaryPageUtil; import com.engine.salary.util.valid.ValidUtil; @@ -520,7 +521,8 @@ public class SalaryArchiveServiceImpl extends Service implements SalaryArchiveSe }); // 3.表数据 - return ExcelUtil.genWorkbookV2(rows, sheetName); +// return ExcelUtil.genWorkbookV2(rows, sheetName); + return ExcelUtilPlus.genWorkbookV2(rows, sheetName); } diff --git a/src/com/engine/salary/util/excel/ExcelParseHelper.java b/src/com/engine/salary/util/excel/ExcelParseHelper.java index 6f7fafb01..c25d8b08d 100644 --- a/src/com/engine/salary/util/excel/ExcelParseHelper.java +++ b/src/com/engine/salary/util/excel/ExcelParseHelper.java @@ -174,6 +174,31 @@ public class ExcelParseHelper { return result; } + /** + * 将sheet数据转为map + * + * @param rowIndex 从哪行开始解析 + * @param headerRowIndex 抽取列数的参考行 + * @return + */ + public static List> parse2Map(Sheet sheet, int rowIndex, int headerRowIndex) { + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(headerRowIndex).getPhysicalNumberOfCells(); // 总列数 + + List sheetHeader = ExcelSupport.getSheetHeader(sheet, headerRowIndex); + + List> result = new ArrayList<>(); + for (; rowIndex < rowCount; rowIndex++) { + Map cellResult = new HashMap<>(); + for (int j = 0; j < cellCount; j++) { + String key = sheetHeader.get(j); + cellResult.put(key, ExcelSupport.getCellValue(sheet, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + /** * 将sheet数据转为List * @@ -195,6 +220,27 @@ public class ExcelParseHelper { return result; } + /** + * 将sheet数据转为List + * @param rowIndex 从哪行开始解析 + * @param headerRowIndex 抽取列数的参考行 + * @return + */ + public static List> parse2List(Sheet sheet, int rowIndex, int headerRowIndex) { + int rowCount = sheet.getPhysicalNumberOfRows(); // 总行数 + int cellCount = sheet.getRow(headerRowIndex).getPhysicalNumberOfCells(); // 总列数 + + List> result = new ArrayList>(); + for (; rowIndex < rowCount; rowIndex++) { + List cellResult = new ArrayList(); + for (int j = 0; j < cellCount; j++) { + cellResult.add(ExcelSupport.getCellValue(sheet, rowIndex, j)); + } + result.add(cellResult); + } + return result; + } + /** * 为对象的每一个属性赋值 diff --git a/src/com/engine/salary/util/excel/ExcelUtilPlus.java b/src/com/engine/salary/util/excel/ExcelUtilPlus.java index b3b2768e5..939e6e78c 100644 --- a/src/com/engine/salary/util/excel/ExcelUtilPlus.java +++ b/src/com/engine/salary/util/excel/ExcelUtilPlus.java @@ -74,12 +74,18 @@ public class ExcelUtilPlus { // 设置title样式 XSSFCellStyle titleCellStyle = workbook.createCellStyle(); XSSFFont titleFont = workbook.createFont(); + titleFont.setBold(true); titleFont.setFontName("仿宋"); titleFont.setFontHeightInPoints((short) 15); titleCellStyle.setFont(titleFont); titleCellStyle.setAlignment(HorizontalAlignment.CENTER); - titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 + titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleCellStyle.setBorderLeft(BorderStyle.THIN); + titleCellStyle.setBorderRight(BorderStyle.THIN); + titleCellStyle.setBorderTop(BorderStyle.THIN); + titleCellStyle.setBorderBottom(BorderStyle.THIN); // 设置主体样式 @@ -89,6 +95,10 @@ public class ExcelUtilPlus { font.setFontHeightInPoints((short) 10);// 设置字体大小 cellStyle.setFont(font);// 选择需要用到的字体格式 cellStyle.setWrapText(true); + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); XSSFSheet sheet = workbook.createSheet(sheetName); //自适应宽度 @@ -97,6 +107,11 @@ public class ExcelUtilPlus { sheet.setDefaultColumnWidth(20); //默认行高 sheet.setDefaultRowHeightInPoints(18); + //遍历设置列宽 + List header = rowList.get(0); + for (int i = 0; i < header.size(); i++) { + sheet.setColumnWidth(i,Math.max(12, header.get(i).toString().length()*4)*256); + } for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); @@ -134,12 +149,18 @@ public class ExcelUtilPlus { // 设置title样式 XSSFCellStyle titleCellStyle = workbook.createCellStyle(); XSSFFont titleFont = workbook.createFont(); + titleFont.setBold(true); titleFont.setFontName("仿宋"); titleFont.setFontHeightInPoints((short) 15); titleCellStyle.setFont(titleFont); titleCellStyle.setAlignment(HorizontalAlignment.CENTER); - titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色 + titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleCellStyle.setBorderLeft(BorderStyle.THIN); + titleCellStyle.setBorderRight(BorderStyle.THIN); + titleCellStyle.setBorderTop(BorderStyle.THIN); + titleCellStyle.setBorderBottom(BorderStyle.THIN); // 设置主体样式 @@ -149,6 +170,10 @@ public class ExcelUtilPlus { font.setFontHeightInPoints((short) 10);// 设置字体大小 cellStyle.setFont(font);// 选择需要用到的字体格式 cellStyle.setWrapText(true); + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); XSSFSheet sheet = workbook.createSheet(sheetName); //自适应宽度 @@ -158,6 +183,12 @@ public class ExcelUtilPlus { //默认行高 sheet.setDefaultRowHeightInPoints(18); + //遍历设置列宽 + List header = rowList.get(0); + for (int i = 0; i < header.size(); i++) { + sheet.setColumnWidth(i,Math.max(12, header.get(i).toString().length()*4)*256); + } + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex); @@ -202,10 +233,11 @@ public class ExcelUtilPlus { // 设置title样式 XSSFCellStyle titleCellStyle = workbook.createCellStyle(); XSSFFont titleFont = workbook.createFont(); + titleFont.setBold(true); titleFont.setFontName("仿宋"); titleFont.setFontHeightInPoints((short) 15); titleCellStyle.setFont(titleFont); - titleCellStyle.setAlignment(HorizontalAlignment.LEFT); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); @@ -214,6 +246,17 @@ public class ExcelUtilPlus { titleCellStyle.setBorderTop(BorderStyle.THIN); titleCellStyle.setBorderBottom(BorderStyle.THIN); + XSSFCellStyle childTitleCellStyle = workbook.createCellStyle(); + childTitleCellStyle.setFont(titleFont); + childTitleCellStyle.setAlignment(HorizontalAlignment.LEFT); + childTitleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 + childTitleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + childTitleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + childTitleCellStyle.setBorderLeft(BorderStyle.THIN); + childTitleCellStyle.setBorderRight(BorderStyle.THIN); + childTitleCellStyle.setBorderTop(BorderStyle.THIN); + childTitleCellStyle.setBorderBottom(BorderStyle.THIN); + // 设置主体样式 @@ -234,6 +277,7 @@ public class ExcelUtilPlus { redFont.setFontName("宋体"); redFont.setFontHeightInPoints((short) 10);// 设置字体大小 redFont.setColor(new XSSFColor(new Color(0xFF3333), null)); + redFont.setBold(true); redCellStyle.setWrapText(true); redCellStyle.setFont(redFont);// 选择需要用到的字体格式 @@ -265,9 +309,10 @@ public class ExcelUtilPlus { rowZeroCell.setCellValue(columnGroupItem.getText().toString()); rowZeroCell.setCellStyle(titleCellStyle); XSSFCell rowOneCell = row1.createCell(startIndex, CellType.STRING); + rowOneCell.setCellValue(columnGroupItem.getText().toString()); rowOneCell.setCellStyle(titleCellStyle); //设置列宽 - sheet.setColumnWidth(startIndex,columnGroupItem.getText().length()*4*256); + sheet.setColumnWidth(startIndex,Math.max(12, columnGroupItem.getText().length()*4)*256); startIndex++; } else { List childrenList = columnGroupItem.getChildren(); @@ -279,7 +324,7 @@ public class ExcelUtilPlus { XSSFCell cell = row0.createCell(startIndex, CellType.STRING); cell.setCellValue(columnGroupItem.getText().toString()); - cell.setCellStyle(titleCellStyle); + cell.setCellStyle(childTitleCellStyle); for (int j = 0; j < childrenList.size(); j++) { WeaTableColumnGroup childrenItem = (WeaTableColumnGroup) childrenList.get(j); @@ -288,7 +333,7 @@ public class ExcelUtilPlus { subHeader.setCellValue(childrenItem.getText().toString()); subHeader.setCellStyle(titleCellStyle); //设置列宽 - sheet.setColumnWidth(startIndex + j,childrenItem.getText().length()*4*256); + sheet.setColumnWidth(startIndex + j,Math.max(12, childrenItem.getText().length()*4)*256); } startIndex += childrenList.size(); diff --git a/src/com/engine/salary/web/SalaryAcctController.java b/src/com/engine/salary/web/SalaryAcctController.java index 8d2db04ef..548553509 100644 --- a/src/com/engine/salary/web/SalaryAcctController.java +++ b/src/com/engine/salary/web/SalaryAcctController.java @@ -537,7 +537,7 @@ public class SalaryAcctController { @Produces(MediaType.APPLICATION_JSON) public String importSalaryAcctResultPreview(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryAcctImportParam param) { User user = HrmUserVarify.getUser(request, response); - return new ResponseResult>(user).run(getSalaryAcctExcelService(user)::preview, param); + return new ResponseResult>(user).run(getSalaryAcctExcelService(user)::previewImportSalaryAcctResult, param); } // **********************************薪资核算结果 end*********************************/ From 31bf7cfd99294b6ccd161afbffb926e6d5f987c6 Mon Sep 17 00:00:00 2001 From: sy Date: Thu, 30 Mar 2023 17:26:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=96=AA=E9=85=AC=E7=B3=BB=E7=BB=9F-?= =?UTF-8?q?=E8=96=AA=E8=B5=84=E6=A0=B8=E7=AE=97=E5=92=8C=E6=A1=A3=E6=A1=88?= =?UTF-8?q?=EF=BC=8Cexcel=E5=AF=BC=E5=87=BA=E8=A1=8C=E9=AB=98=E8=87=AA?= =?UTF-8?q?=E9=80=82=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../salary/util/excel/ExcelUtilPlus.java | 156 +++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/src/com/engine/salary/util/excel/ExcelUtilPlus.java b/src/com/engine/salary/util/excel/ExcelUtilPlus.java index 939e6e78c..0fb1cdab8 100644 --- a/src/com/engine/salary/util/excel/ExcelUtilPlus.java +++ b/src/com/engine/salary/util/excel/ExcelUtilPlus.java @@ -7,7 +7,6 @@ import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; -import java.awt.*; import java.awt.Color; import java.util.Date; import java.util.List; @@ -116,6 +115,9 @@ public class ExcelUtilPlus { for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); if (rowIndex == 0) { @@ -137,7 +139,12 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(o == null ? "" : o.toString()); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } return workbook; } @@ -192,6 +199,9 @@ public class ExcelUtilPlus { for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); if (rowIndex == 0) { @@ -213,7 +223,12 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(String.valueOf(o)); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } if (CollectionUtils.isNotEmpty(comments)) { @@ -226,6 +241,106 @@ public class ExcelUtilPlus { return workbook; } + public static XSSFWorkbook genWorkbookV2(List> rowList, String sheetName, boolean lastRowRed) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setBold(true); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); + titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleCellStyle.setBorderLeft(BorderStyle.THIN); + titleCellStyle.setBorderRight(BorderStyle.THIN); + titleCellStyle.setBorderTop(BorderStyle.THIN); + titleCellStyle.setBorderBottom(BorderStyle.THIN); + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFCellStyle redCellStyle = workbook.createCellStyle(); + XSSFFont redFont = workbook.createFont(); + redFont.setFontName("宋体"); + redFont.setFontHeightInPoints((short) 10);// 设置字体大小 + redFont.setColor(new XSSFColor(new Color(0xFF3333), null)); + redFont.setBold(true); + redCellStyle.setWrapText(true); + redCellStyle.setFont(redFont);// 选择需要用到的字体格式 + + redCellStyle.setBorderLeft(BorderStyle.THIN); + redCellStyle.setBorderRight(BorderStyle.THIN); + redCellStyle.setBorderTop(BorderStyle.THIN); + redCellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + //遍历设置列宽 + List header = rowList.get(0); + for (int i = 0; i < header.size(); i++) { + sheet.setColumnWidth(i,Math.max(12, header.get(i).toString().length()*4)*256); + } + + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + if (lastRowRed && rowIndex == rowList.size() - 1) { + cell.setCellStyle(redCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + } + Object o = infoList.get(cellIndex); + if (o instanceof String) { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o)); + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(o == null ? "" : o.toString()); + } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); + } + row.setHeightInPoints(finalHeight); + } + return workbook; + } public static XSSFWorkbook genWorkbookWithChildTitleColumn(List> rowList, String sheetName, boolean lastRowRed) { XSSFWorkbook workbook = new XSSFWorkbook(); @@ -345,6 +460,9 @@ public class ExcelUtilPlus { for (int rowIndex = 1; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex + 1); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); @@ -371,8 +489,44 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(o == null ? "" : o.toString()); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } return workbook; } + + public static float getFinalHeight(Object o, int width, float finalHeight, float height) { + if (o != null && getStrlength(o.toString()) > width) { + float remainder = getStrlength(o.toString()) % width; + int multiple = getStrlength(o.toString()) / width; + int finalMultiple = remainder > 0 ? (multiple + 1) : multiple; + float compareHeight = height * finalMultiple; + finalHeight = Math.max(finalHeight, compareHeight); + } + return finalHeight; + } + + public static int getStrlength(String str) { + int strLength = 0; + String chinese = "[\u0391-\uFFE5]"; + /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */ + for (int i = 0; i < str.length(); i++) { + /* 从字符串中获取一个字符 */ + String temp = str.substring(i, i + 1); + /* 判断是否为中文字符 */ + if (temp.matches(chinese)) { + /* 中文字符长度为2 */ + strLength += 2; + } else { + /* 其他字符长度为1 */ + strLength += 1; + } + } + return strLength; + } + }