diff --git a/laws-modules/src/main/java/com/jero/modules/laws/customcompare/service/impl/LawsCustomCompareInfoServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/customcompare/service/impl/LawsCustomCompareInfoServiceImpl.java index 60b6bb30..feda9608 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/customcompare/service/impl/LawsCustomCompareInfoServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/customcompare/service/impl/LawsCustomCompareInfoServiceImpl.java @@ -38,9 +38,7 @@ import com.jero.modules.system.entity.SysDictItem; import com.jero.modules.system.service.ISysDictItemService; import com.jero.modules.system.service.ISysDictService; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -347,12 +345,19 @@ public class LawsCustomCompareInfoServiceImpl extends ServiceImpl standardVOS, List featureList, List compareList, List list) { - for (int i = 2; i < compareList.size() + 2; i++) { + private void createTableCellContent(XSSFSheet sheet, List standardVOS, List featureList, List compareList, List list, CellStyle cellStyle) { + for (int i = 2; i <= compareList.size() + 2; i++) { // 创建行 Row indexRow = sheet.createRow(i); // 创建列 Cell indexCell = indexRow.createCell(0); + indexCell.setCellStyle(cellStyle); + if (i == 2){ // 特点列 - createFeatureColumn(standardVOS, featureList, indexRow, indexCell); + createFeatureColumn(standardVOS, featureList, indexRow, indexCell, cellStyle); }else { // 普通列 - createOrdinaryColumn(standardVOS, featureList, compareList, list, i, indexRow, indexCell); + /** + * createOrdinaryColumn(standardVOS, featureList, compareList, list, i, indexRow, indexCell); + */ + indexCell.setCellValue(compareList.get(i - 3).getCellTitle()); + + int index = 0; + for (StandardVO standardVO : standardVOS) { + for (LawsCustomCompareInventory lawsCustomCompareInventory : featureList) { + index += 1; + // 创建列 + Cell cell = indexRow.createCell(index); + cell.setCellStyle(cellStyle); + int finalI = i; + Optional cellOptional = list.stream() + .filter(v -> Objects.equals(lawsCustomCompareInventory.getId(), v.getFeatureId()) + && Objects.equals(standardVO.getId(), v.getStandardId()) + && Objects.equals(compareList.get(finalI - 3).getId(), v.getCompareItemId())) + .findFirst(); + cellOptional.ifPresent(v -> cell.setCellValue(v.getContent())); + } + } } } } - private static void createOrdinaryColumn(List standardVOS, List featureList, List compareList, List list, int i, Row indexRow, Cell indexCell) { - indexCell.setCellValue(compareList.get(i).getCellTitle()); + /** + * + * private static void createOrdinaryColumn(List standardVOS, List featureList, List compareList, List list, int i, Row indexRow, Cell indexCell) { + * indexCell.setCellValue(compareList.get(i - 3).getCellTitle()); + * + * int index = 0; + * for (StandardVO standardVO : standardVOS) { + * for (LawsCustomCompareInventory lawsCustomCompareInventory : featureList) { + * index += 1; + * // 创建列 + * Cell cell = indexRow.createCell(index); + * // cell.setCellStyle(cellStyle); + * Optional cellOptional = list.stream() + * .filter(v -> Objects.equals(lawsCustomCompareInventory.getId(), v.getFeatureId()) + * && Objects.equals(standardVO.getId(), v.getStandardId()) + * && Objects.equals(compareList.get(i - 3).getId(), v.getCompareItemId())) + * .findFirst(); + * cellOptional.ifPresent(v -> cell.setCellValue(v.getContent())); + * } + * } + * } + */ + private static void createFeatureColumn(List standardVOS, List featureList, Row indexRow, Cell indexCell, CellStyle cellStyle) { + indexCell.setCellValue("特点/对比项"); + // 计算列数量 int index = 0; - for (StandardVO standardVO : standardVOS) { + for (int i = 0; i < standardVOS.size(); i++) { for (LawsCustomCompareInventory lawsCustomCompareInventory : featureList) { index += 1; // 创建列 - Cell cell1 = indexRow.createCell(index); - Optional cellOptional = list.stream() - .filter(v -> Objects.equals(lawsCustomCompareInventory.getId(), v.getFeatureId()) - && Objects.equals(standardVO.getId(), v.getCompareItemId())) - .findFirst(); - cellOptional.ifPresent(v -> cell1.setCellValue(v.getContent())); + Cell cell = indexRow.createCell(index); + cell.setCellValue(lawsCustomCompareInventory.getCellTitle()); + cell.setCellStyle(cellStyle); } } } - private static void createFeatureColumn(List standardVOS, List featureList, Row indexRow, Cell indexCell) { - indexCell.setCellValue("特点/对比项"); - // 计算列数量 - int cellSize = featureList.size() * standardVOS.size(); - for (int j = 1; j <= cellSize; j++) { - // 创建列 - Cell cell = indexRow.createCell(j); - int index = j % featureList.size(); - if (index == 0){ - cell.setCellValue(featureList.get(featureList.size() - 1).getCellTitle()); - }else { - cell.setCellValue(featureList.get(index - 1).getCellTitle()); - } - } - } - - private void createTableSecondRow(XSSFSheet sheet, List standardVOS, List featureList) { + private void createTableSecondRow(XSSFSheet sheet, List standardVOS, List featureList, CellStyle cellStyle) { // 创建第二行 Row row = sheet.createRow(1); // 创建列 Cell cell = row.createCell(0); cell.setCellValue("标准号"); + cell.setCellStyle(cellStyle); // 计算第二行文字内容和需要合并的单元格 // 下标 - AtomicInteger index2 = new AtomicInteger(); + AtomicInteger index = new AtomicInteger(); +// // 计算下次合并单元格起始 + AtomicInteger nextMergeFirst = new AtomicInteger(); for (StandardVO standardVO : standardVOS) { - index2.addAndGet(1); + index.addAndGet(1); // 创建列 - Cell indexCell = row.createCell(index2.get()); + Cell indexCell = row.createCell(index.get() + nextMergeFirst.get()); indexCell.setCellValue(standardVO.getStandardNumber()); - + indexCell.setCellStyle(cellStyle); // 特点数量 int featureSize = featureList.size(); // 合并单元格(参数说明:起始行,结束行,起始列,结束列) - CellRangeAddress region = new CellRangeAddress(1, 1, index2.get(), index2.get() + featureSize - 1); - sheet.addMergedRegion(region); + CellRangeAddress region = new CellRangeAddress(1, 1, index.get() + nextMergeFirst.get(), index.get() + featureSize - 1 + nextMergeFirst.get()); + nextMergeFirst.set(featureSize - 1); + sheet.addMergedRegionUnsafe(region); } } - private void createTableHeader(XSSFSheet sheet, Map> countryMap, List featureList) { + private void createTableHeader(XSSFSheet sheet, Map> countryMap, List featureList, CellStyle cellStyle) { // 创建第一行 Row headerRow = sheet.createRow(0); // 创建列 Cell cell = headerRow.createCell(0); cell.setCellValue("国家/地区"); + cell.setCellStyle(cellStyle); if (!countryMap.isEmpty()){ // 下标 AtomicInteger index = new AtomicInteger(); + // 计算下次合并单元格起始 + AtomicInteger nextMergeFirst = new AtomicInteger(); countryMap.forEach((k, v) -> { index.addAndGet(1); // 标准数量 @@ -508,10 +547,12 @@ public class LawsCustomCompareInfoServiceImpl extends ServiceImpl