当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Thinking about data governance after Didi fines
- Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
- 剑指offer专项突击版第15天
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- STM32HAL库修改Hal_Delay为us级延时
- MySQL数据库必会的增删查改操作(CRUD)
- The third is the code to achieve
- 专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
- 问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
- Can't load /home/Iot/.rnd into RNG
猜你喜欢

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

XSS靶场(三)prompt to win

Minesweeper game - C language

C language from entry to such as soil, the data store

MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)

ERP Production Operation Control Kingdee

已解决:不小心卸载pip后(手动安装pip的两种方式)

ERP生产作业控制 金蝶

Explanation of

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
随机推荐
PWN ROP
open failed: EACCES (Permission denied)
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
C language from entry to such as soil, the data store
Safety 20220718
重磅 | 基金会为白金、黄金、白银捐赠人授牌
ABC D - Distinct Trio(k元组的个数
pom文件成橘红色未加载的解决方案
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
重磅 | 开放原子校源行活动正式启动
Basic knowledge of mysql (2)
qlib自动化quant
【py脚本】批量二值化处理图像
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Visual studio shortcuts that improve efficiency, summary (updated from time to time)
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
Understanding of the presence of a large number of close_wait states
扫雷游戏(c语言写)
unity2d game
【云原生】DevOps(五):集成Harbor