一、概述
最近公司需要做一个需求,通过excel上传病例信息,并将病例信息进行归档和整理;该需求可以简化为excel模板下载和excel上传并解析归档。既然知道需求了,找excel的操作工具jar包吧,发现以前常用的poi需要写的代码太多,但是时间紧急,没办法只能找新的工具。easypoi 在读写数据的时候,优先是先将数据写入内存,优点是读写性能非常高,但是当数据量很大的时候,会出现oom,当然它也提供了 sax 模式的读写方式,需要调用特定的方法实现。病例的信息量可能会很大,很有可能会造成内存溢出,没办法,再找吧!easyexcel 基于sax模式进行读写数据,不会出现oom情况,程序有过高并发场景的验证,因此程序运行比较稳定,相对于 easypoi 来说,虽然读写性能稍慢,但是更符合我目前的开发场景。
二、easypoi实例
easypoi 的亮点就是基于注解实体类来实现导入、导出excel,使用起来非常简单!
1. jar包引入
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
2. 创建实体注解类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserEntity {
@Excel(name = "姓名")
private String name;
@Excel(name = "年龄")
private int age;
@Excel(name = "操作时间",format="yyyy-MM-dd HH:mm:ss", width = 20.0)
private Date time;
}
3. 导出业务
public static void main(String[] args) throws Exception {
List<UserEntity> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
UserEntity userEntity = new UserEntity();
userEntity.setName(