网站首页 > 博客文章 正文
一、概述
springboot集成easyexcel实现通用异步导出平台
基础代码可参考前面几篇文章,本文介绍下easyexcel如何根据指定模板导出数据(模板事先上传到服务器)
二、实战
异步导出框架代码参考前面的文章,根据指定模板导出excel代码改动不大,此处只给出核心代码:ExportTemplate.exportDataByTemplate:
public void exportDataByTemplate(ExportSo exportSo, String taskId) {
String businessTypeCode = exportSo.getBusinessTypeCode();
TaskDefinition taskDefinition = TaskDefinition.getTaskDefinition(businessTypeCode);
if (null == taskDefinition) {
throw new IllegalArgumentException("导出业务类型编码异常!");
}
String fileName = taskDefinition.getFileName();
String filePath = "D:\\excel\\" + fileName + ".xlsx";
ExportTaskEntity updateExportTaskEntity = new ExportTaskEntity();
updateExportTaskEntity.setTaskId(taskId);
updateExportTaskEntity.setFilePath(filePath);
updateExportTaskEntity.setUpdateTime(new Date());
updateExportTaskEntity.setTaskStatusCode(TaskStatusEnum.SUCCESS.getCode());
updateExportTaskEntity.setTaskStatusName(TaskStatusEnum.SUCCESS.getDesc());
try {
// 按照模板导出:withTemplate方法指定文件URL
String templateUrl = "D:\\excel\\学生信息模板.xlsx";
ExcelWriter excelWriter = EasyExcelFactory.write(filePath).withTemplate(templateUrl).build();
// 执行具体导出业务逻辑(查询DB、ES数据等),写入EXCEL
String exportServiceName = taskDefinition.getExportService();
ExportBaseService exportBaseService = (ExportBaseService) SpringUtils.getBean(exportServiceName);
WriteSheet writeSheet = EasyExcelFactory.writerSheet(fileName).build();
exportBaseService.export(exportSo, excelWriter, writeSheet);
excelWriter.finish();
} catch (Exception e) {
updateExportTaskEntity.setTaskStatusCode(TaskStatusEnum.FAIL.getCode());
updateExportTaskEntity.setTaskStatusName(TaskStatusEnum.FAIL.getDesc());
log.error("导出任务异常 任务ID: {} 导出参数: {} 异常信息: ", taskId, JSON.toJSONString(exportSo), e);
}
// 更新导出任务
exportTaskMapper.updateExportTask(updateExportTaskEntity);
}
withTemplate方法有3个重载方法:InputStream、File、filePathName可供选择
public ExcelWriterBuilder withTemplate(InputStream templateInputStream) {
writeWorkbook.setTemplateInputStream(templateInputStream);
return this;
}
public ExcelWriterBuilder withTemplate(File templateFile) {
writeWorkbook.setTemplateFile(templateFile);
return this;
}
public ExcelWriterBuilder withTemplate(String pathName) {
return withTemplate(new File(pathName));
}
猜你喜欢
- 2024-09-29 一日一技:使用XlsxWriter模块在Excel工作表中绘制组合图表
- 2024-09-29 Hutool Java工具类库导出Excel,超级简单
- 2024-09-29 有了这个开源工具后,我五点就下班了
- 2024-09-29 pandas写excel报错IllegalCharacterError解决方案
- 2024-09-29 python从入门到实践,文件读写与Excel操作
- 2024-09-29 Python读写EXCEL文件常用方法大全
- 2024-09-29 太棒了!Python和Excel过了这么久终于可以互通了
- 2024-09-29 EasyExcel实现Excel验证标红备注下载
- 2024-09-29 Python 操作 Excel 表格从简单到高级用法
- 2024-09-29 Excel和Python终于可以互通了(python怎么与excel衔接)
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)