Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import excel error when same sheet name #2131

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,17 +43,27 @@ public File getFile(DownloadCreateParam downloadParams, String path, String file
ViewService viewService = Application.getBean(ViewService.class);

Workbook workbook = POIUtils.createEmpty();
Map<String, Integer> 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);
viewExecuteParam.setPageInfo(PageInfo.builder().pageNo(1).pageSize(orgSettingService.getDownloadRecordLimit(view.getOrgId())).build());
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.");
Expand Down