当前位置:网站首页>EasyExcel的简单读取操作
EasyExcel的简单读取操作
2022-08-01 23:48:00 【嗯嗯好的呢!】
EasyExcel的简单读取操作
- Java领域解析、生成Excel比较有名的框架有Apache poi、 jxl等。但他们都存在一个严重的问题就是
非常的耗内存。如果你的系统并发量不大的话可能还行,但是一旦并发上来后一定会OOM或
者JVM频繁的full gc。 - EasyExcel是阿里巴巴开源的一个excel处理框架, 以使用简单、节省内存著称。 EasyExcel能大大减
少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一
行行读取数据,逐个解析。 - EasyExcel采用一行一行的解析模式,并将一行的解析结果以观察者的模式通知处理
(AnalysisEventListener)。
摘自官网
导入依赖
这个版本之间有个对应关系,不然会报错。
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- easyexcel依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoData {
// 设置表头名称,index表示的是编号的索引
@ExcelProperty(value = "学生编号",index = 0)
private Integer sno;
@ExcelProperty(value = "学生姓名",index = 1)
private String sname;
}
用EasyExcel进行写操作测试
/**
* easyExcel写操作测试
*/
@Test
public void write() {
String outputFile = "E:\write11.xlsx";
EasyExcel.write(outputFile, DemoData.class).sheet("学生列表").doWrite(TestEasyExcel.getData());
}
private static List<DemoData> getData() {
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String name = UUID.randomUUID().toString().replace("-", "").substring(0, 5);
list.add(new DemoData(i, name));
}
return list;
}


用EasyExcel进行读操作
读的时候要ExcelListener监听器。
创建ExcelListener类继承AnalysisEventListener带上excel表格中的对象
/**
* Created with IntelliJ IDEA.
* @Author: pzx
* @Date: 2022/01/30 15:46
* @Version:1.0
*/
public class ExcelListener extends AnalysisEventListener<DemoData> {
// 一行一行去读取excel中的内容(表头不会去读取)
@Override
public void invoke(DemoData data, AnalysisContext context) {
System.out.println("********" + data);
}
// 读取表头中的内容
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头 " + headMap);
}
// 读取完成之后做的内容
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}
代码测试
/**
* easyExcel读操作测试
*/
@Test
public void read() {
String outputFile = "E:\write11.xlsx";
// 加上继承了AnalysisEventListener类的监听器
ExcelListener excelListener = new ExcelListener();
EasyExcel.read(outputFile,DemoData.class, excelListener).sheet().doRead();
}

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training

A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
![[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
![Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights](/img/f1/9824f32dd4fe4b3e94af3f945b1801.png)
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights

C language - branch statement and loop statement

使用Jenkins做持续集成,这个知识点必须要掌握

技术分享 | 接口测试中如何使用Json 来进行数据交互 ?

2022 6th Strong Net Cup Part WP

【图像融合】基于加权和金字塔实现图像融合附matlab代码

分享一份接口测试项目(非常值得练手)
随机推荐
经典文献阅读之--DLO
在linux下MySQL的常用操作命令
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
Enterprise firewall management, what firewall management tools are there?
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
如何用Redis实现分布式锁?
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
6134. 找到离给定两个节点最近的节点-力扣双百代码
@Resource和@Autowired的区别
【图像融合】基于加权和金字塔实现图像融合附matlab代码
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
如何更好的理解的和做好工作?
信息系统项目管理师必背核心考点(五十七)知识管理工具
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
ELK日志采集
检查 Oracle 版本的 7 种方法
Avoid hidden text when loading fonts
Additional Features for Scripting