|
@@ -1,9 +1,7 @@
|
|
|
package com.chelvc.framework.export.support;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
import java.io.Serializable;
|
|
|
import java.time.Duration;
|
|
|
import java.util.Collection;
|
|
@@ -17,6 +15,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.base.context.ThreadContextHolder;
|
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
|
+import com.chelvc.framework.common.function.Adapter;
|
|
|
import com.chelvc.framework.common.function.Executor;
|
|
|
import com.chelvc.framework.common.util.AssertUtils;
|
|
|
import com.chelvc.framework.common.util.ExcelUtils;
|
|
@@ -143,6 +142,11 @@ public class DefaultExportHandler implements ExportHandler, ApplicationListener<
|
|
|
return ObjectUtils.ifNull(this.exporting(id), Exporting::getStatus);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void save(long id, @NonNull Workbook workbook) throws IOException {
|
|
|
+ ExcelUtils.save(workbook, new File(this.directory, String.valueOf(id)));
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void download(long id, @NonNull HttpServletResponse response) throws IOException {
|
|
|
Exporting exporting = this.exporting(id);
|
|
@@ -154,9 +158,8 @@ public class DefaultExportHandler implements ExportHandler, ApplicationListener<
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public <T> long export(@NonNull String filename, @NonNull BiConsumer<Long, Integer> listener,
|
|
|
- @NonNull BiFunction<Integer, Integer, Collection<T>> provider,
|
|
|
- @NonNull ExcelUtils.Writer<T> writer, @NonNull String... titles) {
|
|
|
+ public long export(@NonNull String filename, @NonNull Adapter<Long, Integer> adapter,
|
|
|
+ @NonNull BiConsumer<Long, Integer> listener) {
|
|
|
// 生成导出任务ID
|
|
|
long id = RedisContextHolder.identity();
|
|
|
|
|
@@ -167,16 +170,8 @@ public class DefaultExportHandler implements ExportHandler, ApplicationListener<
|
|
|
ThreadContextHolder.run(() -> {
|
|
|
int volume;
|
|
|
try {
|
|
|
- // 将数据写入工作薄
|
|
|
- Workbook workbook = new SXSSFWorkbook();
|
|
|
- volume = ExcelUtils.write(workbook, provider, writer, titles);
|
|
|
-
|
|
|
- // 保存导出文件
|
|
|
- File file = new File(this.directory, String.valueOf(id));
|
|
|
- try (OutputStream output = new FileOutputStream(file)) {
|
|
|
- workbook.write(output);
|
|
|
- workbook.close();
|
|
|
- }
|
|
|
+ // 导出任务处理回调
|
|
|
+ volume = adapter.apply(id);
|
|
|
|
|
|
// 更新导出任务完成状态
|
|
|
this.exporting(id, filename, Exporting.Status.COMPLETED, this.duration);
|
|
@@ -197,4 +192,16 @@ public class DefaultExportHandler implements ExportHandler, ApplicationListener<
|
|
|
});
|
|
|
return id;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <T> long export(@NonNull String filename, @NonNull BiConsumer<Long, Integer> listener,
|
|
|
+ @NonNull BiFunction<Integer, Integer, Collection<T>> provider,
|
|
|
+ @NonNull ExcelUtils.Writer<T> writer, @NonNull String... titles) {
|
|
|
+ return this.export(filename, id -> {
|
|
|
+ Workbook workbook = new SXSSFWorkbook();
|
|
|
+ int volume = ExcelUtils.write(workbook, provider, writer, titles);
|
|
|
+ this.save(id, workbook);
|
|
|
+ return volume;
|
|
|
+ }, listener);
|
|
|
+ }
|
|
|
}
|