当前位置:网站首页>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、测试
边栏推荐
- JS解混淆-AST还原案例
- Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
- Flask introductory learning tutorial
- 发送激活邮件「建议收藏」
- Basic JSON usage
- 一本通2074:【21CSPJ普及组】分糖果(candy)
- JuiceFS 在多云存储架构中的应用 | 深势科技分享
- 关于ETL的两种架构(ETL架构和ELT架构)
- 第 1 章 一大波数正在靠近——排序
- CGLIB源码易懂解析
猜你喜欢
随机推荐
在“企业通讯录”的盲区,融云的边界与分寸
反射机制篇
JS–比想象中简单
leetcode 38. 外观数列
C. Omkar and Baseball
Linux 配置MySQL
leetcode:332. 重新安排行程
In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
国内手机厂商曾为它大打出手,如今它却最先垮台……
FileZilla搭建FTP服务器图解教程
SecureCRT background color
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
为什么这么多人都想当产品经理?
1215 – Cannot add foreign key constraint
月薪5K的运维小白如何成为月薪5W的高级架构师?
openGauss数据库基本操作(超详细)
typedef和#define的花里胡哨的用法
R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
three.js镂空圆球拖拽变形js特效
leetcode 刷题日记 计算右侧小于当前元素的个数