当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
- 6134. Find the closest node to the given two nodes - force double hundred code
- 6132. All the elements in the array is equal to zero - quick sort method
- Programmer is still short of objects? A new one is enough
- Getting started with IDEA is enough to read this article
- Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
- [Camp Experience Post] 2022 Cybersecurity Summer Camp
- Classical Literature Reading--DLO
- yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
猜你喜欢

分享一份接口测试项目(非常值得练手)

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

企业防护墙管理,有什么防火墙管理工具?

Quartus 使用 tcl 文件快速配置管脚

UI自动化测试框架搭建-标记性能较差用例

Chrome书签插件,让你实现高效整理

Appears in oozie on CDH's hue, error submitting Coordinator My Schedule

CDH6 Hue to open a "ASCII" codec can 't encode characters

Making a Simple 3D Renderer

洞见云原生微服务及微服务架构浅析
随机推荐
GIF制作-灰常简单的一键动图工具
Use Jenkins for continuous integration, this knowledge point must be mastered
Flink Yarn Per Job - 提交流程一
recursion: method calls itself
Additional Features for Scripting
Chapter 12 End-User Task As Shell Scripts
Docker搭建Mysql主从复制
Convert LocalDateTime to Date type
辛普森悖论
Architecture basic concept and nature of architecture
Programmer is still short of objects? A new one is enough
Classical Literature Reading--DLO
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
高效工作文档产出归类
多御安全浏览器android版更新至1.7,改进加密协议
Chapter 11 Working with Dates and Times
numpy.around
使用Ganache、web3.js和remix在私有链上部署并调用合约