From dc26f7be11bc4b624195507596227c3006645d83 Mon Sep 17 00:00:00 2001 From: kanlon Date: Sun, 2 Apr 2023 18:17:35 +0800 Subject: [PATCH] fix: import excel error when same sheet name --- .../impl/AttachmentExcelServiceImpl.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/datart/server/service/impl/AttachmentExcelServiceImpl.java b/server/src/main/java/datart/server/service/impl/AttachmentExcelServiceImpl.java index f6f1883b4..7d846d226 100644 --- a/server/src/main/java/datart/server/service/impl/AttachmentExcelServiceImpl.java +++ b/server/src/main/java/datart/server/service/impl/AttachmentExcelServiceImpl.java @@ -10,13 +10,19 @@ import datart.server.base.params.DownloadCreateParam; import datart.server.base.params.ViewExecuteParam; import datart.server.common.PoiConvertUtils; -import datart.server.service.*; +import datart.server.service.AttachmentService; +import datart.server.service.DataProviderService; +import datart.server.service.OrgSettingService; +import datart.server.service.ViewService; +import datart.server.service.VizService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.stereotype.Service; import java.io.File; +import java.util.HashMap; +import java.util.Map; @Service("excelAttachmentService") @Slf4j @@ -37,6 +43,7 @@ public File getFile(DownloadCreateParam downloadParams, String path, String file ViewService viewService = Application.getBean(ViewService.class); Workbook workbook = POIUtils.createEmpty(); + Map sheetNameMap = new HashMap<>(16); for (int i = 0; i < downloadParams.getDownloadParams().size(); i++) { ViewExecuteParam viewExecuteParam = downloadParams.getDownloadParams().get(i); View view = viewService.retrieve(viewExecuteParam.getViewId(), false); @@ -44,10 +51,19 @@ public File getFile(DownloadCreateParam downloadParams, String path, String file Dataframe dataframe = dataProviderService.execute(downloadParams.getDownloadParams().get(i)); String chartConfigStr = vizService.getChartConfigByVizId(viewExecuteParam.getVizType(), viewExecuteParam.getVizId()); POISettings poiSettings = PoiConvertUtils.covertToPoiSetting(chartConfigStr, dataframe); - String sheetName = StringUtils.isNotBlank(viewExecuteParam.getVizName()) ? viewExecuteParam.getVizName() : "Sheet"+i; + String sheetName = StringUtils.isNotBlank(viewExecuteParam.getVizName()) ? viewExecuteParam.getVizName() : "Sheet" + i; + + //avoid same sheetName + if (sheetNameMap.containsKey(sheetName)) { + sheetNameMap.put(sheetName, sheetNameMap.get(sheetName) + 1); + sheetName += sheetNameMap.get(sheetName); + } else { + sheetNameMap.put(sheetName, 0); + } + POIUtils.withSheet(workbook, sheetName, dataframe, poiSettings); } - path = generateFileName(path,fileName,attachmentType); + path = generateFileName(path, fileName, attachmentType); File file = new File(path); POIUtils.save(workbook, file.getPath(), true); log.info("create excel file complete.");