当前位置:网站首页>EasyExcel使用
EasyExcel使用
2022-08-09 22:01:00 【㏒灵韵№】
自行搭建好SpringBoot工程
EasyExcel简介
1、是什么 EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。
2、为什么 以使用简单、节省内存著称,EasyExcel采用一行一行的解析模式,并将一行的解析结果以观察者的模式通知处理
EasyExcel写操作
1、添加依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
2、在测试目录下创建实体

@Data
public class DemoData {
//设置表头名称
@ExcelProperty("学生编号")
private int sno;
//设置表头名称
@ExcelProperty("学生姓名")
private String sname;
}
3、创建测试类
@Test
public void writeTest(){
// 写法1
String fileName = "D:\\test\\201021\\write.xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("学生列表").doWrite(data());
}
//循环设置要添加的数据,最终封装到list集合中
private static List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setSno(i);
data.setSname("张三"+i);
list.add(data);
}
return list;
}

EasyExcel读操作
1、读取数据文档
列固定,行不固定
2、修改实体
@Data
public class DemoData {
//设置表头名称
@ExcelProperty(value = "学生编号",index = 0)
private int sno;
//设置表头名称
@ExcelProperty(value = "学生姓名",index = 1)
private String sname;
}
3、创建监听器
public class ExcelListener extends AnalysisEventListener<DemoData> {
//读取每一行数据
@Override
public void invoke(DemoData demoData, AnalysisContext analysisContext) {
//在这里面完成相关业务,比如添加数据
System.out.println(demoData);
}
//数据读取完做的事
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
4、实现方法
@Test
public void readTest(){
String fileName = "D:\\test\\201021\\write.xlsx";
EasyExcel.read(fileName, DemoData.class,new ExcelListener()).sheet().doRead();
}
5、测试
边栏推荐
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- Under the NVM node installation;The node environment variable configuration
- navicat 快捷键
- 反射机制篇
- leetcode:321. 拼接最大数
- Easyui 表单验证「建议收藏」
- leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
- Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
- Blender程序化建模简明教程【PCG】
- R语言ggplot2可视化:使用ggpubr包的ggerrorplot函数可视化误差线(可视化不同水平均值点以及se标准误差)、设置add参数为dotplot添加点阵图
猜你喜欢

Common commands and technical function modules of Metasploit

leetcode:323. 无向图中连通分量的数目

Socket发送缓冲区接收缓冲区快问快答

小程序+自定义插件的关键性

Flask introductory learning tutorial

Analyze the Add() method in Fragment management from the source code

leetcode 39. 组合总和(完全背包问题)

Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut

从产品角度看 L2 应用:为什么说这是一个游乐场?

肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!
随机推荐
json case
B. Applejack and Storages
十步以内,用小程序快速生成App!
navicat 快捷键
JS Deobfuscation - AST Restoration Case
聊聊SQL语句中 DDL 、DML 、DQL 、DCL 分别是什么
typedef和#define的花里胡哨的用法
每日一R「02」所有权与 Move 语义
js数组对象去重
C 在函数声明前加typedef
腾讯继续挥舞降本增效“大刀”,外包员工免费餐饮福利被砍了
p5.js实现的炫酷星体旋转动画
一文让你快速了解隐式类型转换【整型提升】!
web 面试高频考点 —— 性能优化篇(手写防抖、手写节流、XXS攻击、XSRF攻击)
A1. Prefix Flip (Easy Version)
xctf攻防世界 Web高手进阶区 shrine
月薪5K的运维小白如何成为月薪5W的高级架构师?
迅为瑞芯微RK3399开发板设置Buildroot文件系统测试MYSQL允许远程访问
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview