当前位置:网站首页>EasyExcel的简单读取操作
EasyExcel的简单读取操作
2022-07-31 04:47:00 【geejkse_seff】
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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
- MySQL数据库增删改查(基础操作命令详解)
- Safety 20220715
- 30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
- MySQL数据库必会的增删查改操作(CRUD)
- [R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
- unity2d game
- ABC D - Distinct Trio(k元组的个数
- ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
- 【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
猜你喜欢
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
【云原生】DevOps(五):集成Harbor
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
随机推荐
【云原生】DevOps(五):集成Harbor
binom二项分布,
Explanation of
pom文件成橘红色未加载的解决方案
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
Create componentized development based on ILRuntime hot update
VScode+ESP32快速安装ESP-IDF插件
ENSP,划分VLAN、静态路由,三层交换机综合配置
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
Reinforcement learning: from entry to pit to shit
高斯分布及其极大似然估计
Safety 20220718
three.js 制作3D相册
Thinking about data governance after Didi fines
ERP生产作业控制 金蝶
Heavyweight | The Open Atomic School Source Line activity was officially launched
[py script] batch binarization processing images
VScode+ESP32 quickly install ESP-IDF plugin
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)