|
@@ -3,6 +3,8 @@ package com.sundata.product.rwa.calc.service;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.date.TimeInterval;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.sundata.common.util.DBExecutor;
|
|
|
import com.sundata.common.util.FileUtil;
|
|
@@ -15,6 +17,8 @@ import com.sundata.product.rwa.util.CollectionToMapConverter;
|
|
|
import com.sundata.product.rwa.util.DataUtil;
|
|
|
import org.apache.commons.text.StringSubstitutor;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellUtil;
|
|
|
+import org.apache.poi.ss.util.SheetUtil;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.jxls.builder.JxlsOutputFile;
|
|
|
import org.jxls.transform.poi.JxlsPoiTemplateFillerBuilder;
|
|
@@ -108,7 +112,7 @@ public class ReportCalcService {
|
|
|
|
|
|
Map<String, List<ReportCalculationModel>> selectCalcReportDatas = service.selectCalcReportData(reportDefinitionModel.getReportNo());
|
|
|
// 这张报表需要公式计算的 计算单元,配置中,需要明确,一旦公式类型,必须配置Excel来源公式 字典:UNIT_STYLE_TYPE
|
|
|
- List<ReportCalculationModel> fillInCalcs = selectCalcReportDatas.get("FillIn").stream().filter(e -> "1".equals(e.getUnitStyleType())).toList();
|
|
|
+ List<ReportCalculationModel> fillInCalcs = new ArrayList<>(selectCalcReportDatas.get("FillIn").stream().filter(e -> "1".equals(e.getUnitStyleType())).toList());
|
|
|
File tempTmpFile = createTempReport(reportDefinitionModel, calcIndex, dataDate, context);
|
|
|
tempTmpFiles.add(tempTmpFile);
|
|
|
log.debug("复制临时模板文件内容到 mainTmpWorkbook 文件中,并将配置的Excel公式,其他报表来源公式均配置到 mainTmpWorkbook 中");
|
|
@@ -123,33 +127,36 @@ public class ReportCalcService {
|
|
|
for (int j = tempSheet.getFirstRowNum(); j < tempSheet.getLastRowNum(); j++) {
|
|
|
int finalJ = j;
|
|
|
List<ReportCalculationModel> thisRowNumModel = thisSheetNumModel.stream().filter(e -> finalJ == (e.getReportUnitRow())).toList();
|
|
|
- Row tempRow = tempSheet.getRow(j);
|
|
|
+ Row tempRow = CellUtil.getRow(j,tempSheet);
|
|
|
if (tempRow != null) {
|
|
|
Row mainTmpRow = mainTmpSheet.createRow(j);
|
|
|
for (int k = tempRow.getFirstCellNum(); k < tempRow.getLastCellNum(); k++) {
|
|
|
int finalK = k;
|
|
|
- List<ReportCalculationModel> thisCellNumModel = thisSheetNumModel.stream().filter(e -> finalK == (e.getReportUnitCell())).toList();
|
|
|
- Cell tempCell = tempRow.getCell(k);
|
|
|
- Cell mainTmpCell = null;
|
|
|
+ List<ReportCalculationModel> thisCellNumModel = thisRowNumModel.stream().filter(e -> finalK == (e.getReportUnitCell())).toList();
|
|
|
+ Cell tempCell = CellUtil.getCell(tempRow,k);
|
|
|
+ Cell mainTmpCell = CellUtil.getCell(mainTmpRow,k);;
|
|
|
+ if (thisCellNumModel.size() == 1) {
|
|
|
+ log.debug("读取该报表配置中的手动配置公式的单元格");
|
|
|
+ // 将对应配置的参数处理为公式内容
|
|
|
+ mainTmpCell.setCellFormula(thisCellNumModel.get(0).getUnitCalcInfo());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
if (tempCell != null) {
|
|
|
- mainTmpCell = mainTmpRow.createCell(k);
|
|
|
switch (tempCell.getCellType()) {
|
|
|
case STRING -> mainTmpCell.setCellValue(tempCell.getStringCellValue());
|
|
|
case NUMERIC -> mainTmpCell.setCellValue(tempCell.getNumericCellValue());
|
|
|
- case FORMULA -> mainTmpCell.setCellFormula(tempCell.getCellFormula());
|
|
|
+ case FORMULA -> {
|
|
|
+ if (StrUtil.isEmpty(tempCell.getCellFormula())) {
|
|
|
+ mainTmpCell.setCellFormula("");
|
|
|
+ }else {
|
|
|
+ mainTmpCell.setCellFormula(tempCell.getCellFormula());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
default -> mainTmpCell.setCellValue("");
|
|
|
}
|
|
|
}
|
|
|
- if (thisCellNumModel.size() == 1) {
|
|
|
- log.debug("读取该报表配置中的手动配置公式的单元格");
|
|
|
- if (mainTmpRow.getCell(k) == null) {
|
|
|
- mainTmpCell = mainTmpRow.getCell(k);
|
|
|
- } else {
|
|
|
- mainTmpCell = mainTmpRow.createCell(k);
|
|
|
- }
|
|
|
- // 将对应配置的参数处理为公式内容
|
|
|
- mainTmpCell.setCellFormula(thisCellNumModel.get(0).getUnitCalcInfo());
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -315,25 +322,26 @@ public class ReportCalcService {
|
|
|
if (sheet == null) {
|
|
|
sheet = workbook.createSheet();
|
|
|
}
|
|
|
- Row row = sheet.getRow(reportCalculationModel.getReportUnitRow());
|
|
|
- if (row == null) {
|
|
|
- row = sheet.createRow(reportCalculationModel.getReportUnitRow());
|
|
|
- }
|
|
|
- Cell cell = row.createCell(reportCalculationModel.getReportUnitCell());
|
|
|
- if (cell == null) {
|
|
|
- cell = row.createCell(reportCalculationModel.getReportUnitCell());
|
|
|
- }
|
|
|
- if ("1".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ Row row = CellUtil.getRow(reportCalculationModel.getReportUnitRow(),sheet);
|
|
|
+ Cell cell = CellUtil.getCell(row,reportCalculationModel.getReportUnitCell());
|
|
|
+ if ("1".equals(reportCalculationModel.getUnitStyleType())) {
|
|
|
// 如果单元格类型是公式
|
|
|
cell.setCellFormula(reportCalculationModel.getUnitCalcInfo());
|
|
|
- } else if ("2".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ } else if ("2".equals(reportCalculationModel.getUnitStyleType())) {
|
|
|
// 如果单元格类型是字符串
|
|
|
cell.setCellValue(String.valueOf(reportUnitParamCalcModel.getValue()));
|
|
|
- } else if ("3".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ } else if ("3".equals(reportCalculationModel.getUnitStyleType())) {
|
|
|
// 如果单元格类型是数字
|
|
|
- cell.setCellValue((Double) DataUtil.getDataDefault(reportUnitParamCalcModel.getValue(), Double.class));
|
|
|
+ String value = String.valueOf(reportUnitParamCalcModel.getValue());
|
|
|
+ if ("null".equals(value)) {
|
|
|
+ value = "0.0";
|
|
|
+ }
|
|
|
+ cell.setCellValue(NumberUtil.parseDouble(value,0.0));
|
|
|
+ } else {
|
|
|
+ cell.setCellValue("");
|
|
|
}
|
|
|
}
|
|
|
+ workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
|
|
|
workbook.write(Files.newOutputStream(tempTmpFilePath.toPath()));
|
|
|
workbook.close();
|
|
|
} catch (IOException e) {
|