导入流程
导出流程
当然其中还有很多其它逻辑要处理,比如出了非业务异常,如何让用户知道,处理进度如何让用户知道,需要事务怎么办。要处理以上这些问题也并非那么容易。
那么有没有好用的开源组件呢?一款使用起来很简单的异步excel组件asyncexcel
1、asyncexcel基于阿里的easyexcel包装,抽取异步骨架,不改变easyexcel的特性
2、有个小坑就是@excelproperty注解的index无法使用了,所以编写excel model的时候需要注意编写顺序需要于excel中的表头顺序一致。
引入包
maven中央库搜索
<dependency>
<groupId>com.asyncexcelgroupId>
<artifactId>async-excel-springboot-starterartifactId>
<version>1.0.0version>
dependency>
内置mybatis-plus orm框架所以还需要引入mybatis-plus-starter
初始化数据库
drop table if exists excel_task;
CREATE TABLE `excel_task` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`type` tinyint(2) NOT NULL COMMENT '类型:1-导入,2-导出',
`status` tinyint(2) NOT NULL DEFAULT 0 COMMENT '状态:0-初始,1-进行中,2-完成,3-失败',
`estimate_count` int(20) NOT NULL DEFAULT 0 COMMENT '预估总记录数',
`total_count` bigint(20) NOT NULL DEFAULT 0 COMMENT '实际总记录数',
`success_count` bigint(20) NOT NULL DEFAULT 0 COMMENT '成功记录数',
`failed_count` bigint(20) NOT NULL DEFAULT 0 COMMENT '失败记录数',
`file_name` varchar(200) DEFAULT NULL COMMENT '文件名',
`file_url` varchar(500) DEFAULT NULL COMMENT '文件路径',
`failed_file_url` varchar(500) DEFAULT NULL COMMENT '失败文件路径',
`failed_message` varchar(255) DEFAULT NULL COMMENT '失败消息',
`start_time` datetime DEFAULT NULL COMMENT '开始时间',
`end_time` datetime DEFAULT NULL COMMENT '结束时间',
`tenant_code` varchar(50) default NULL COMMENT '租户编码',
`create_user_code` varchar(50) default NULL COMMENT '用户编码',
`business_code` varchar(50) default NULL COMMENT '业务编码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='导入导出任务';
配置数据源
spring.excel.datasource.url=jdbc:mysql://localhost:3306/async-excel?serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&&useCursorFetch=true&&rewriteBatchedStatements=true
spring.excel.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.excel.datasource.password=root
spring.excel.datasource.username=root
引入你的pom文件然后在main函数上注解@EenableAsyncExcel就可以完成了以上所说的所有过程,你只需要专注于你的业务代码就可以了。
然后编写业务处理类
导出示例
head model
@Data
public class UserExportModel extends ExportRow {
@ExcelProperty("用户编码")
private String userCode;
@ExcelProperty("用户姓名")
private String userName;
@ExcelProperty("手机号")
private String mobile;
@ExcelProperty("备注")
private String remarks;
}
业务处理类需要实现ExportHandler接口 添加@ExcelHandle注解
@ExcelHandle
public class UserExportHandler implements ExportHandler<UserExportModel> {
@Autowired
IUserService userService;
@Override
public ExportPage<UserExportModel> exportData(int startPage, int limit, DataExportParam dataExportParam) {
IPage<User> iPage = new Page<>(startPage, limit);
IPage page = userService.page(iPage);
List<UserExportModel> list = ExportListUtil.transform(page.getRecords(), UserExportModel.class);
ExportPage<UserExportModel> result = new ExportPage<>();
result.setTotal(page.getTotal());
result.setCurrent(page.getCurrent());
result.setSize(page.getSize());
result.setRecords(list);
return result;
}
}
添加controller
//导出最简示例
@PostMapping("/exports")
public Long exports(){
DataExportParam dataExportParam=new DataExportParam()
.setExportFileName("用户导出")
.setLimit(5)
.setHeadClass(UserExportModel.class);
return excelService.doExport(UserExportHandler.class,dataExportParam);
}
最后效果如下,前端代码还需要你自己写的哈

示例项目https://github.com/2229499815/async-excel-sample.git
更多详情请查看开源项目https://github.com/2229499815/async-excel.git