Merge pull request #9407 from dataease/pr@dev@fixExportData

fix(数据集): 导出数据集数据出错
This commit is contained in:
taojinlong 2024-04-28 15:16:43 +08:00 committed by GitHub
commit 715837c715
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -656,27 +656,20 @@ public class ExportCenterService {
wb.close(); wb.close();
} else { } else {
Integer totalPage = (totalCount / extractPageSize) + (totalCount % extractPageSize > 0 ? 1 : 0); Integer totalPage = (totalCount / extractPageSize) + (totalCount % extractPageSize > 0 ? 1 : 0);
List<DatasetTableField> fields = new ArrayList<>(); List<DatasetTableField> fields = new ArrayList<>();
Workbook wb = new SXSSFWorkbook();
FileOutputStream fileOutputStream = new FileOutputStream(dataPath + "/" + request.getFilename() + ".xlsx");
Sheet detailsSheet = wb.createSheet("数据");
for (Integer p = 1; p < totalPage + 1; p++) { for (Integer p = 1; p < totalPage + 1; p++) {
Map<String, Object> previewData = getPreviewData(request, p, extractPageSize, extractPageSize, null, tree); Map<String, Object> previewData = getPreviewData(request, p, extractPageSize, extractPageSize, null, tree);
List<Map<String, Object>> data = (List<Map<String, Object>>) previewData.get("data"); List<Map<String, Object>> data = (List<Map<String, Object>>) previewData.get("data");
if (p == 1L) { if (p == 1L) {
Workbook wb = new SXSSFWorkbook();
// Sheet
Sheet detailsSheet = wb.createSheet("数据");
//给单元格设置样式
CellStyle cellStyle = wb.createCellStyle(); CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont(); Font font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 12); font.setFontHeightInPoints((short) 12);
//设置字体加粗
font.setBold(true); font.setBold(true);
//给字体设置样式
cellStyle.setFont(font); cellStyle.setFont(font);
//设置单元格背景颜色
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//设置单元格填充样式(使用纯色背景颜色填充)
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
fields = (List<DatasetTableField>) previewData.get("fields"); fields = (List<DatasetTableField>) previewData.get("fields");
List<String> header = new ArrayList<>(); List<String> header = new ArrayList<>();
@ -700,10 +693,9 @@ public class ExportCenterService {
if (rowData != null) { if (rowData != null) {
for (int j = 0; j < rowData.size(); j++) { for (int j = 0; j < rowData.size(); j++) {
Cell cell = row.createCell(j); Cell cell = row.createCell(j);
if (i == 0) {// 头部 if (i == 0) {
cell.setCellValue(rowData.get(j)); cell.setCellValue(rowData.get(j));
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
//设置列的宽度
detailsSheet.setColumnWidth(j, 255 * 20); detailsSheet.setColumnWidth(j, 255 * 20);
} else { } else {
if ((fields.get(j).getDeType().equals(DeTypeConstants.DE_INT) || fields.get(j).getDeType() == DeTypeConstants.DE_FLOAT) && StringUtils.isNotEmpty(rowData.get(j))) { if ((fields.get(j).getDeType().equals(DeTypeConstants.DE_INT) || fields.get(j).getDeType() == DeTypeConstants.DE_FLOAT) && StringUtils.isNotEmpty(rowData.get(j))) {
@ -720,10 +712,6 @@ public class ExportCenterService {
} }
} }
} }
try (FileOutputStream outputStream = new FileOutputStream(dataPath + "/" + request.getFilename() + ".xlsx")) {
wb.write(outputStream);
}
wb.close();
} else { } else {
List<List<String>> details = new ArrayList<>(); List<List<String>> details = new ArrayList<>();
for (Map<String, Object> obj : data) { for (Map<String, Object> obj : data) {
@ -734,9 +722,25 @@ public class ExportCenterService {
} }
details.add(row); details.add(row);
} }
EasyExcel.write(dataPath + "/" + request.getFilename() + p + ".xlsx").withTemplate(dataPath + "/" + request.getFilename() + ".xlsx").sheet("数据").doWrite(details); int lastNum = detailsSheet.getLastRowNum();
FileUtil.del(dataPath + "/" + request.getFilename() + ".xlsx"); for (int i = 0; i < details.size(); i++) {
new File(dataPath + "/" + request.getFilename() + p + ".xlsx").renameTo(new File(dataPath + "/" + request.getFilename() + ".xlsx")); Row row = detailsSheet.createRow(i + lastNum + 1);
List<String> rowData = details.get(i);
if (rowData != null) {
for (int j = 0; j < rowData.size(); j++) {
Cell cell = row.createCell(j);
if ((fields.get(j).getDeType().equals(DeTypeConstants.DE_INT) || fields.get(j).getDeType() == DeTypeConstants.DE_FLOAT) && StringUtils.isNotEmpty(rowData.get(j))) {
try {
cell.setCellValue(Double.valueOf(rowData.get(j)));
} catch (Exception e) {
LogUtil.warn("export excel data transform error");
}
} else {
cell.setCellValue(rowData.get(j));
}
}
}
}
} }
exportTask.setExportStatus("IN_PROGRESS"); exportTask.setExportStatus("IN_PROGRESS");
double exportRogress = (double) ((double) p / (double) totalPage); double exportRogress = (double) ((double) p / (double) totalPage);
@ -745,6 +749,9 @@ public class ExportCenterService {
exportTask.setExportPogress(formattedResult); exportTask.setExportPogress(formattedResult);
exportTaskMapper.updateByPrimaryKey(exportTask); exportTaskMapper.updateByPrimaryKey(exportTask);
} }
wb.write(fileOutputStream);
fileOutputStream.close();
wb.close();
} }
exportTask.setExportPogress("100"); exportTask.setExportPogress("100");
exportTask.setExportStatus("SUCCESS"); exportTask.setExportStatus("SUCCESS");