网站转化率低,网站加速工具,建筑导航网站,会展设计专业学什么一、介绍
在上篇文章中#xff0c;我们介绍了 easypoi 工具实现 excel 文件的导入导出。
本篇我们继续深入介绍另一款更优秀的 excel 工具库#xff1a;easyexcel 。
二、easyexcel
easyexcel 是阿里巴巴开源的一款 excel 解析工具#xff0c;底层逻辑也是基于 apache p…一、介绍
在上篇文章中我们介绍了 easypoi 工具实现 excel 文件的导入导出。
本篇我们继续深入介绍另一款更优秀的 excel 工具库easyexcel 。
二、easyexcel
easyexcel 是阿里巴巴开源的一款 excel 解析工具底层逻辑也是基于 apache poi 进行二次开发的。不同的是再读写数据的时候采用 sax 模式一行一行解析在并发量很大的情况下依然能稳定运行
下面我们就一起来了解一下这款新起之秀
4.1、首先添加依赖包
dependenciesdependencygroupIdcom.alibaba/groupIdartifactIdeasyexcel/artifactIdversion2.2.6/version/dependency!--常用工具库--dependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion29.0-jre/version/dependency
/dependencies4.2、采用注解导出导入
easyexcel 同样也支持采用注解方式进行导出、导入
首先我们创建一个实体类UserEntity其中ExcelProperty注解表示导出文件的头部信息。
public class UserEntity {ExcelProperty(value 姓名)private String name;ExcelProperty(value 年龄)private int age;DateTimeFormat(yyyy-MM-dd HH:mm:ss)ExcelProperty(value 操作时间)private Date time;//set、get省略
}接着我们来编写导出服务
public static void main(String[] args) {ListUserEntity dataList new ArrayList();for (int i 0; i 10; i) {UserEntity userEntity new UserEntity();userEntity.setName(张三 i);userEntity.setAge(20 i);userEntity.setTime(new Date(System.currentTimeMillis() i));dataList.add(userEntity);}EasyExcel.write(/Users/hello/Documents/easyexcel-user1.xls, UserEntity.class).sheet(用户信息).doWrite(dataList);
}导出的文件预览如下 对应的导入操作也很简单源码如下
public static void main(String[] args) {String filePath /Users/hello/Documents/easyexcel-user1.xls;ListDemoData list EasyExcel.read(filePath).head(UserEntity.class).sheet().doReadSync();System.out.println(JSONArray.toJSONString(list));
}运行程序输出结果如下
[{age:20,name:张三0,time:1616920360000},{age:21,name:张三1,time:1616920360000},{age:22,name:张三2,time:1616920360000},{age:23,name:张三3,time:1616920360000},{age:24,name:张三4,time:1616920360000},{age:25,name:张三5,time:1616920360000},{age:26,name:张三6,time:1616920360000},{age:27,name:张三7,time:1616920360000},{age:28,name:张三8,time:1616920360000},{age:29,name:张三9,time:1616920360000}]4.3、自定义数据结构导出导入
easyexcel 同样也支持自定义数据结构导出导入excel。
自定义数据导出 excel
public static void main(String[] args) {//表头ListListString headList new ArrayList();headList.add(Lists.newArrayList(姓名));headList.add(Lists.newArrayList(年龄));headList.add(Lists.newArrayList(操作时间));//数据体ListListObject dataList new ArrayList();for (int i 0; i 10; i) {ListObject data new ArrayList();data.add(张三 i);data.add(20 i);data.add(new Date(System.currentTimeMillis() i));dataList.add(data);}EasyExcel.write(/Users/hello/Documents/easyexcel-user2.xls).head(headList).sheet(用户信息).doWrite(dataList);
}导入 excel
public static void main(String[] args) {String filePath /Users/panzhi/Documents/easyexcel-user2.xls;UserDataListener userDataListener new UserDataListener();EasyExcel.read(filePath, userDataListener).sheet().doRead();System.out.println(表头 JSONArray.toJSONString(userDataListener.getHeadList()));System.out.println(数据体 JSONArray.toJSONString(userDataListener.getDataList()));
}运行程序输出结果如下
表头[{0:姓名,1:年龄,2:操作时间}]
数据体[{0:张三0,1:20,2:2021-03-28 16:31:39},{0:张三1,1:21,2:2021-03-28 16:31:39},{0:张三2,1:22,2:2021-03-28 16:31:39},{0:张三3,1:23,2:2021-03-28 16:31:39},{0:张三4,1:24,2:2021-03-28 16:31:39},{0:张三5,1:25,2:2021-03-28 16:31:39},{0:张三6,1:26,2:2021-03-28 16:31:39},{0:张三7,1:27,2:2021-03-28 16:31:39},{0:张三8,1:28,2:2021-03-28 16:31:39},{0:张三9,1:29,2:2021-03-28 16:31:39}]更多的 api 操作可以访问 easyexcel - 接口文档
三、小结
总体来说easypoi和easyexcel都是基于apache poi进行二次开发的。
不同点在于
1、easypoi 在读写数据的时候优先是先将数据写入内存优点是读写性能非常高但是当数据量很大的时候会出现oom当然它也提供了 sax 模式的读写方式需要调用特定的方法实现。
2、easyexcel 基于sax模式进行读写数据不会出现oom情况程序有过高并发场景的验证因此程序运行比较稳定相对于 easypoi 来说读写性能稍慢
easypoi 与 easyexcel 还有一点区别在于easypoi 对定制化的导出支持非常的丰富如果当前的项目需求并发量不大、数据量也不大但是需要导出 excel 的文件样式千差万别那么我推荐你用 easypoi反之使用 easyexcel
四、参考
1、apache poi - 接口文档
2、easypoi - 接口文档
3、easyexcel - 接口文档
写到最后
不会有人刷到这里还想白嫖吧点赞对我真的非常重要在线求赞。加个关注我会非常感激
本文已整理到技术笔记中此外笔记内容还涵盖 Spring、Spring Boot/Cloud、Dubbo、JVM、集合、多线程、JPA、MyBatis、MySQL、微服务等技术栈。 需要的小伙伴可以点击 技术笔记 获取