|
@@ -26,6 +26,8 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.ExcelWriter;
|
|
|
+import com.alibaba.excel.converters.longconverter.LongStringConverter;
|
|
|
import com.alibaba.excel.util.FileUtils;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
@@ -43,7 +45,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class ExportService {
|
|
|
- private final static int PAGE_SIZE = 5000;
|
|
|
+ private final static int PAGE_SIZE = 50000;
|
|
|
|
|
|
@Value("${blade.file.formDir}")
|
|
|
private String formDir;
|
|
@@ -73,7 +75,12 @@ public class ExportService {
|
|
|
threadPool.execute(() -> {
|
|
|
log.info("ExportService::exporting... task id is " + task.getId());
|
|
|
long start = System.currentTimeMillis();
|
|
|
- String error = handle(task);
|
|
|
+ File excelFile = new File(getFilePath(task.getFileName()));
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(excelFile)
|
|
|
+ .registerConverter(new LongStringConverter())
|
|
|
+ .head(buidlHeader(task))
|
|
|
+ .build();
|
|
|
+ String error = handle(excelWriter, task);
|
|
|
if (StrUtil.isBlankIfStr(error)) {
|
|
|
task.setStatus(DataExportTaskStatus.success.getCode());
|
|
|
log.info("ExportService::export success, task id is " + task.getId());
|
|
@@ -82,6 +89,9 @@ public class ExportService {
|
|
|
log.info("ExportService::export failure, task id is " + task.getId());
|
|
|
deleteCache(task);
|
|
|
}
|
|
|
+ if (excelWriter != null) {
|
|
|
+ excelWriter.finish();
|
|
|
+ }
|
|
|
long end = System.currentTimeMillis();
|
|
|
float takeUpTime = (end - start) / 1000.f;
|
|
|
task.setExt(error);
|
|
@@ -93,7 +103,7 @@ public class ExportService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private String handle(DataExportTask task) {
|
|
|
+ private String handle(ExcelWriter excelWriter, DataExportTask task) {
|
|
|
String error = null;
|
|
|
try {
|
|
|
File excelFile = new File(getFilePath(task.getFileName()));
|
|
@@ -115,7 +125,7 @@ public class ExportService {
|
|
|
page = pageData.getCurrent() + 1;
|
|
|
List<JSONObject> dataList = pageData.getRecords();
|
|
|
total += dataList.size();
|
|
|
- error = writeToExcel(task, dataList);
|
|
|
+ error = writeToExcel(excelWriter, task, dataList);
|
|
|
if (StrUtil.isBlankIfStr(error)) {
|
|
|
// 写入excel成功
|
|
|
} else {
|
|
@@ -135,7 +145,8 @@ public class ExportService {
|
|
|
return error;
|
|
|
}
|
|
|
|
|
|
- private String writeToExcel(DataExportTask task, List<JSONObject> retList) {
|
|
|
+ private String writeToExcel(ExcelWriter excelWriter, DataExportTask task,
|
|
|
+ List<JSONObject> retList) {
|
|
|
try {
|
|
|
List<List<Object>> excelList = new ArrayList<List<Object>>();
|
|
|
List<RelationField> fieldList = JsonUtil.getJsonToList(task.getRule(), RelationField.class);
|
|
@@ -150,10 +161,7 @@ public class ExportService {
|
|
|
});
|
|
|
excelList.add(rowList);
|
|
|
});
|
|
|
- EasyExcel.write(getFilePath(task.getFileName()))
|
|
|
- .head(buidlHeader(task))
|
|
|
- .sheet(task.getName())
|
|
|
- .doWrite(excelList);
|
|
|
+ excelWriter.write(excelList, EasyExcel.writerSheet(task.getName()).build());
|
|
|
} catch (Exception e) {
|
|
|
return e.getMessage();
|
|
|
}
|
|
@@ -215,7 +223,15 @@ public class ExportService {
|
|
|
log.info("headerList is " + JSON.toJSONString(headerList));
|
|
|
log.info("excelList is " + JSON.toJSONString(excelList));
|
|
|
|
|
|
+ // EasyExcel.write(excelFile.getPath()).head(headerList).sheet("模板").doWrite(excelList);
|
|
|
File excelFile = new File("D://www/upload/form/test.xlsx");
|
|
|
- EasyExcel.write(excelFile.getPath()).head(headerList).sheet("模板").doWrite(excelList);
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(excelFile)
|
|
|
+ .registerConverter(new LongStringConverter())
|
|
|
+ .head(headerList)
|
|
|
+ .build();
|
|
|
+ excelWriter.write(excelList, EasyExcel.writerSheet("哈哈哈").build());
|
|
|
+ if (excelWriter != null) {
|
|
|
+ excelWriter.finish();
|
|
|
+ }
|
|
|
}
|
|
|
}
|