fix(自定义对比): 导出完善

This commit is contained in:
yjz
2023-10-27 18:46:45 +08:00
parent fca3188cc7
commit 4f59ce460b
@@ -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<LawsCustomComp
// 列宽
sheet.setDefaultColumnWidth(20);
//样式
CellStyle cellStyle = workbook.createCellStyle();
// 水平、垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 创建表头内容
createTableHeader(sheet, countryMap, featureList);
createTableHeader(sheet, countryMap, featureList, cellStyle);
// 创建第二行内容
createTableSecondRow(sheet, standardVOS, featureList);
createTableSecondRow(sheet, standardVOS, featureList, cellStyle);
// 创建单元格内容
createTableCellContent(sheet, standardVOS, featureList, compareList, list);
createTableCellContent(sheet, standardVOS, featureList, compareList, list, cellStyle);
// excel临时保存到本地并在同级创建文件夹
generateExcel(workbook, fileName);
@@ -361,6 +366,7 @@ public class LawsCustomCompareInfoServiceImpl extends ServiceImpl<LawsCustomComp
//删除指定文件夹
FileUtils.deleteFolders(exportExcelTempPath);
}catch (Exception e){
log.error(e.getMessage());
throw new JeroBootException("导出失败!");
}
}
@@ -415,90 +421,123 @@ public class LawsCustomCompareInfoServiceImpl extends ServiceImpl<LawsCustomComp
}
}
private void createTableCellContent(XSSFSheet sheet, List<StandardVO> standardVOS, List<LawsCustomCompareInventory> featureList, List<LawsCustomCompareInventory> compareList, List<LawsCustomCompareCell> list) {
for (int i = 2; i < compareList.size() + 2; i++) {
private void createTableCellContent(XSSFSheet sheet, List<StandardVO> standardVOS, List<LawsCustomCompareInventory> featureList, List<LawsCustomCompareInventory> compareList, List<LawsCustomCompareCell> 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<LawsCustomCompareCell> 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<StandardVO> standardVOS, List<LawsCustomCompareInventory> featureList, List<LawsCustomCompareInventory> compareList, List<LawsCustomCompareCell> list, int i, Row indexRow, Cell indexCell) {
indexCell.setCellValue(compareList.get(i).getCellTitle());
/**
*
* private static void createOrdinaryColumn(List<StandardVO> standardVOS, List<LawsCustomCompareInventory> featureList, List<LawsCustomCompareInventory> compareList, List<LawsCustomCompareCell> 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<LawsCustomCompareCell> 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<StandardVO> standardVOS, List<LawsCustomCompareInventory> 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<LawsCustomCompareCell> 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<StandardVO> standardVOS, List<LawsCustomCompareInventory> 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<StandardVO> standardVOS, List<LawsCustomCompareInventory> featureList) {
private void createTableSecondRow(XSSFSheet sheet, List<StandardVO> standardVOS, List<LawsCustomCompareInventory> 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<String, List<StandardVO>> countryMap, List<LawsCustomCompareInventory> featureList) {
private void createTableHeader(XSSFSheet sheet, Map<String, List<StandardVO>> countryMap, List<LawsCustomCompareInventory> 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<LawsCustomComp
// 单元格合并数量
int merge = standardSize * featureSize;
// 首先先填充内容,再合并单元格
Cell indexCell = headerRow.createCell(index.get());
Cell indexCell = headerRow.createCell(index.get() + nextMergeFirst.get());
indexCell.setCellValue(k);
indexCell.setCellStyle(cellStyle);
// 合并单元格(参数说明:起始行,结束行,起始列,结束列)
CellRangeAddress region = new CellRangeAddress(0, 0, index.get(), index.get() + merge - 1);
CellRangeAddress region = new CellRangeAddress(0, 0, index.get() + nextMergeFirst.get(), index.get() + nextMergeFirst.get() + merge - 1);
nextMergeFirst.set(merge - 1);
sheet.addMergedRegion(region);
});
}