From a0bfd8861f2b7bdda084642be951fc4063021c2f Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Wed, 1 Nov 2023 13:42:24 +0800 Subject: [PATCH] =?UTF-8?q?excel=E6=9F=A5=E8=AF=A2=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ReportExcelServiceImpl.java | 80 +++++++++++-------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java index 911d1ada..1551f51d 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java @@ -27,16 +27,15 @@ import com.anjiplus.template.gaea.business.modules.reportexcel.util.CellType; import com.anjiplus.template.gaea.business.modules.reportexcel.util.XlsUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.itextpdf.text.*; -import com.itextpdf.text.Font; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; -import com.itextpdf.tool.xml.XMLWorkerHelper; -import com.lowagie.text.DocumentException; -import io.swagger.models.auth.In; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,14 +43,15 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.xhtmlrenderer.pdf.ITextRenderer; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.*; import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; /** @@ -422,10 +422,11 @@ public class ReportExcelServiceImpl implements ReportExcelService { } private String getStringValue(Cell cell) { - if (cell == null) + if (cell == null) { return ""; - else + } else { return cell.toString(); + } } /** * 解析报表数据,动态插入列表数据和对象数据 @@ -436,9 +437,11 @@ public class ReportExcelServiceImpl implements ReportExcelService { String setParam = reportExcelDto.getSetParam(); List dbObjectList = (List) JSON.parse(jsonStr); - if (dbObjectList != null && dbObjectList.size() > 0) { + if (dbObjectList != null && !dbObjectList.isEmpty()) { + //数据集数据缓存 + Map setMap = new HashMap<>(); for (int x = 0; x < dbObjectList.size(); x++) { - analysisSheetCellData(dbObjectList.get(x), setParam); + analysisSheetCellData(dbObjectList.get(x), setParam, setMap); } } //fastjson $ref 循环引用 @@ -511,7 +514,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { * * @param dbObject */ - private void analysisSheetCellData(JSONObject dbObject, String setParam) { + private void analysisSheetCellData(JSONObject dbObject, String setParam, Map setMap) { //清空data值 dbObject.remove("data"); //celldata是一个一维数组 @@ -525,7 +528,9 @@ public class ReportExcelServiceImpl implements ReportExcelService { dbObject.getJSONArray("celldata").clear(); //获取配置项中的合并属性 JSONObject merge = dbObject.getJSONObject("config").getJSONObject("merge"); - if(merge != null) merge.clear(); + if(merge != null) { + merge.clear(); + } //定义存储每一列动态扩展的行数 Map colAddCntMap = new HashMap<>(); // 遍历已存在的单元格,查看是否存在动态参数 @@ -534,7 +539,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { JSONObject cellObj = celldata.get(i); //fastjson深拷贝问题 String cellStr = cellObj.toJSONString(); - analysisCellData(cellObj,setParam,colAddCntMap,cellStr,merge, dbObject,cellDataMap); + analysisCellData(cellObj,setParam,colAddCntMap,cellStr,merge, dbObject,cellDataMap, setMap); } } } @@ -544,7 +549,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { * @param cellObject */ public void analysisCellData(JSONObject cellObject,String setParam,Map colAddCntMap,String cellStr, - JSONObject merge,JSONObject dbObject,Map cellDataMap){ + JSONObject merge,JSONObject dbObject,Map cellDataMap, Map setMap){ //获取行号 Integer cellR = cellObject.getInteger("r"); //获取列数 @@ -562,11 +567,11 @@ public class ReportExcelServiceImpl implements ReportExcelService { //处理动态单元格 String v = cellObject.getJSONObject("v").getString("v"); DataSetDto dataSet = getDataSet(v, setParam); - handleDynamicCellObject(dataSet,v,cellStr,cnt,cellR,cellC,merge,dbObject,colAddCntMap); + handleDynamicCellObject(dataSet,v,cellStr,cnt,cellR,cellC,merge,dbObject,colAddCntMap, setMap); break; default: //处理静态单元格 - handleStaticCellObject(cellStr,dbObject,cnt,cellR,cellC,cellDataMap,setParam,merge,colAddCntMap,cellType); + handleStaticCellObject(cellStr,dbObject,cnt,cellR,cellC,cellDataMap,setParam,merge,colAddCntMap,cellType, setMap); break; } } @@ -584,9 +589,15 @@ public class ReportExcelServiceImpl implements ReportExcelService { * @param colAddCntMap */ public void handleDynamicCellObject(DataSetDto dataSet,String v,String cellStr,int cnt,int r,int c, - JSONObject merge,JSONObject dbObject,Map colAddCntMap){ + JSONObject merge,JSONObject dbObject,Map colAddCntMap, Map setMap){ //获取动态数据 - OriginalDataDto originalDataDto = dataSetService.getData(dataSet); + OriginalDataDto originalDataDto; + if (dataSet != null && setMap.containsKey(dataSet.getSetCode())) { + originalDataDto = setMap.get(dataSet.getSetCode()); + }else { + originalDataDto = dataSetService.getData(dataSet); + setMap.put(dataSet.getSetCode(), originalDataDto); + } List cellDynamicData = originalDataDto.getData(); if(cellDynamicData != null){ @@ -643,7 +654,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { */ public void handleStaticCellObject(String cellStr,JSONObject dbObject,int cnt,int r,int c, Map cellDataMap,String setParam, - JSONObject merge,Map colAddCntMap,CellType cellType){ + JSONObject merge,Map colAddCntMap,CellType cellType,Map setMap){ //转字符串,解决深拷贝问题 JSONObject addCellData = JSONObject.parseObject(cellStr); int rows = 0; @@ -655,7 +666,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { break; case STATIC_AUTO: //获取静态单元格右侧动态单元格的总行数 - rows = getRightDynamicCellRows(addCellData,cellDataMap,setParam,rows,cellType); + rows = getRightDynamicCellRows(addCellData,cellDataMap,setParam,rows,cellType, setMap); initCellPosition(addCellData,cnt,merge); if(rows > 1){ //需要把这个静态扩展单元格 改变为 静态合并扩展单元格,就是增加合并属性 mc 以及merge配置 @@ -673,7 +684,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { break; case STATIC_MERGE_AUTO: //获取静态单元格右侧动态单元格的总行数 - rows = getRightDynamicCellRows(addCellData,cellDataMap,setParam,rows,cellType); + rows = getRightDynamicCellRows(addCellData, cellDataMap, setParam, rows, cellType, setMap); initCellPosition(addCellData,cnt,merge); if(rows > 0){ //需要修改单元格mc中的rs @@ -724,7 +735,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { * @param cellType * @return */ - public int getRightDynamicCellRows(JSONObject addCellData,Map cellDataMap,String setParam,int sumRows,CellType cellType){ + public int getRightDynamicCellRows(JSONObject addCellData,Map cellDataMap,String setParam,int sumRows,CellType cellType,Map setMap){ //1、获取此单元格右侧关联的所有单元格 List rightCellList = getRightDynamicCell(addCellData,cellDataMap,cellType); //2、循环获取每个单元格的扩展行数 @@ -735,7 +746,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { case STATIC_AUTO: case STATIC_MERGE_AUTO: //递归查找 - sumRows = getRightDynamicCellRows(rightCell,cellDataMap,setParam,sumRows,rightCellType); + sumRows = getRightDynamicCellRows(rightCell,cellDataMap,setParam,sumRows,rightCellType, setMap); break; case BLACK: case STATIC: @@ -745,7 +756,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { sumRows += rightCell.getJSONObject("v").getJSONObject("mc").getInteger("rs"); break; default: - List cellDynamicData = getDynamicDataList(rightCell.getJSONObject("v").getString("v"),setParam); + List cellDynamicData = getDynamicDataList(rightCell.getJSONObject("v").getString("v"),setParam, setMap); if(cellDynamicData != null && cellDynamicData.size() > 1){ int size = cellDynamicData.size(); sumRows += size; @@ -880,13 +891,18 @@ public class ReportExcelServiceImpl implements ReportExcelService { * @param setParam * @return */ - private List getDynamicDataList(String v, String setParam){ + private List getDynamicDataList(String v, String setParam, Map setMap){ if(StringUtils.isNotBlank(v)){ DataSetDto dataSet = getDataSet(v,setParam); if(dataSet != null){ - OriginalDataDto originalDataDto = dataSetService.getData(dataSet); - List cellDynamicData = originalDataDto.getData(); - return cellDynamicData; + OriginalDataDto originalDataDto; + if (setMap.containsKey(dataSet.getSetCode())) { + originalDataDto = setMap.get(dataSet.getSetCode()); + }else { + originalDataDto = dataSetService.getData(dataSet); + setMap.put(dataSet.getSetCode(), originalDataDto); + } + return originalDataDto.getData(); }else{ return null; } @@ -907,7 +923,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { // 查询条件 if (setParamJson.containsKey(dto.getSetCode())) { JSONObject paramCondition = setParamJson.getJSONObject(dto.getSetCode()); - paramCondition.forEach(map::put); + map.putAll(paramCondition); } dto.setContextData(map); }