SpringBoot(六) - 阿里巴巴的EasyExcel
1、依赖
<!-- 阿里EasyExcel start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.7</version>
</dependency>
2、写入Excel
2.1 实体
@Data
public class Student {
//学号
@ExcelProperty("学号")
private Integer id;
// 姓名
@ExcelProperty("姓名")
private String name;
// 年龄
@ExcelProperty("年龄")
private Integer age;
// 班级
@ExcelProperty("班级")
private String classRoom;
// 性别
@ExcelProperty("性别")
private String sex;
// 院校
@ExcelProperty("院校")
private String graduate;
// 毕业时间
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
@ExcelProperty("毕业时间")
private Date graduateTime;
// 手机号
@ExcelProperty("手机号")
private String phone;
//@NumberFormat("#.#") //保留1位小数
//@ExcelProperty(value = "薪资", index = 3) //设置表图和指定的列, 将工资放在 第四列
//@ExcelIgnore 忽略字段,比如密码
}
2.2 实体的数据监听器
@Slf4j
public class StudentDataListener extends AnalysisEventListener<Student> {
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5; //实际操作的时候看情况修改
List<Student> list = new ArrayList<>();
/**
* 这个每一条数据解析都会来调用
*
* @param data
* one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(Student data, AnalysisContext context) {
log.info("解析到一条数据:{}", data);
list.add(data);
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
if (list.size() >= BATCH_COUNT) {
log.info("存数据库");
// 批量插入
// 存储完成清理 list
list.clear();
}
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("所有数据解析完成!");
}
//为了方便获取读取的数据
public List<Student> getList() {
return list;
}
}
2.3 03版本的Excel写入
@Test
public void testExcel03(){
//要写入的文件的路径
String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel03_student.xls";
// 如果这里想使用03 则 传入excelType参数即可
//获取学生信息,实际应用中从数据库中查询出来即可,list集合
Student student = Student.builder().id(19).name("huayu").age(19)
.classRoom("KH96").sex("男").graduate("金科")
.graduateTime(new Date()).phone("13501020304").build();
List<Student> studentList = new ArrayList<>();
studentList.add(student);
//将数据写入文件
EasyExcel.write(fileName, Student.class)
.excelType(ExcelTypeEnum.XLS)
.sheet("学生信息")
.doWrite(studentList);
log.info("学生信息写入完成!!!{}",student);
}
测试结果:

2.4 07版本的Excel 写入
@Test
public void testExcel07(){
//要写入的文件的路径
String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student.xls";
//获取学生信息
studentList.add(student);
//07版写入文件
EasyExcel.write(fileName, Student.class)
.sheet("学生信息")
.doWrite(studentList);
log.info("学生信息写入完成!!!{}",student);
}
测试结果:

2.5 写入多个sheet
@Test
public void testWriteSheets(){
String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";
//获取学生信息
studentList.add(student);
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
ExcelWriter excelWriter = EasyExcel.write(fileName, Student.class).build();
WriteSheet sheet;
for (int i = 0; i < 2; i++) {
sheet = EasyExcel.writerSheet(i, "学生信息"+(i+1)).build();
excelWriter.write(studentList,sheet);
}
excelWriter.finish();
log.info("学生信息写入完成!!!{}",student);
}
测试结果:

3、读出Excel
3.1 doRead()
3.1.1 sheet1中的数据
sheet1:

3.1.2 doRead() 方法读取
//读取Excel中的学生信息
@Test
public void testReadExcel(){
String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";
// 实例化一个数据监听器
StudentDataListener studentDataListener = new StudentDataListener();
//第一种
//读取后的数据放进list中,所以可以从list中获取数据,(这里的数据是我们处理过的,最多返回5条的数据,可以根据实际境况修改)
EasyExcel.read(fileName, Student.class,studentDataListener)
.sheet() //默认读取第一个sheet
.doRead();
List<Student> list = studentDataListener.getList();
log.info("------ 读取后的数据放进list中,所以可以从list中获取数据,最多返回5条的数据 ------");
list.forEach(ersonData->{
log.info(ersonData.toString());
});
}
3.1.3 测试结果:

3.1.4 分析 doRead() 方法
//没有任何数据返回
public void doRead() {
if (this.excelReader == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
} else {
this.excelReader.read(new ReadSheet[]{this.build()});
this.excelReader.finish();
}
}
3.2 doReadSync()
3.2.1 sheet2中的数据
sheet2:

3.2.2 doReadSync() 方法读取
//读取Excel中的学生信息
@Test
public void testReadExcel(){
String fileName = "D:\\KEGONGCHANG\\DaiMa\\IDEA\\KH96\\SpringBoot\\SpringBoot\\springboot-03-asyztimer\\excel\\excel07_student_sheets.xlsx";
// 实例化一个数据监听器
StudentDataListener studentDataListener = new StudentDataListener();
//第二种
log.info("------ 同步获取数据,读取到的所有数据 ------");
//同步获取数据,读取到的所有数据
List<Student> list2 = EasyExcel.read(fileName, Student.class,studentDataListener)
.sheet(1) //读取第二个sheet
.doReadSync();
list2.forEach(ersonData->{
log.info(ersonData.toString());
});
}
3.2.3 测试结果:

3.2.4 分析 doReadSync() 方法
//会将读取到的数据返回
public <T> List<T> doReadSync() {
if (this.excelReader == null) {
throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
} else {
SyncReadListener syncReadListener = new SyncReadListener();
this.registerReadListener(syncReadListener);
this.excelReader.read(new ReadSheet[]{this.build()});
this.excelReader.finish();
return syncReadListener.getList();
}
}
SpringBoot(六) - 阿里巴巴的EasyExcel的更多相关文章
- SpringBoot集成阿里巴巴Druid监控
druid是阿里巴巴开源的数据库连接池,提供了优秀的对数据库操作的监控功能,本文要讲解一下springboot项目怎么集成druid. 本文在基于jpa的项目下开发,首先在pom文件中额外加入drui ...
- SpringBoot 配置阿里巴巴Druid连接池
在Spring Boot下默认提供了若干种可用的连接池(dbcp,dbcp2, tomcat, hikari),当然并不支持Druid,Druid来自于阿里系的一个开源连接池,它提供了非常优秀的监控功 ...
- SpringBoot(六) SpirngBoot与Mysql关系型数据库
pom.xml文件的配置 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- spring-boot(六) 邮件服务
学习文章来自:springboot(十):邮件服务 简单使用 1.pom包配置 pom包里面添加spring-boot-starter-mail包引用 <dependencies> < ...
- SpringBoot 六问
1.什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立 ...
- springboot(六)SpringBoot问题汇总
SpringBoot2.0整合Mybatis,取datetime数据类型字段出来时,发现少了8小时. 过程:mysql中注册时间查询出来结果是正确的,只是java程序运行出来后显示少了8小时.经前辈指 ...
- springboot(六):如何优雅的使用mybatis
这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis.到网上找了一下关于spring boot ...
- SpringBoot(六)_AOP统一处理请求
什么是AOP AOP 是一种编程范式,与编程语言无关: 将通用逻辑从业务逻辑中分离出来(假如你的业务是一条线,我们不在业务线上写一行代码就能完成附加任务!我们会把代码写在其他的地方): 具体实现 (1 ...
- SpringBoot (六) :如何优雅的使用 mybatis
原文出处: 纯洁的微笑 这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis.到网上找了一下关 ...
- springboot(六)-使用shiro
前提 写之前纠结了一番,这一节放在shiro里面还是springboot里面.后来想了下,还是放springboot里吧,因为这里没有shiro的新东西,只有springboot添加了新东西的使用. ...
随机推荐
- CSPS2024题目总结
T1 决斗 签到题,考场上10min就做出来了. 我的方法是排序之后贪心打怪,就是用尽量小的怪去打现在场上最小的怪.用一个同侧双指针实现. \(O(nlogn)\). 另一种方法注意到了值域很小,可以 ...
- Flink Checkpoint & Savepoint
Flink checkpoint Checkpoint是Flink实现容错机制最核心的功能,能够根据配置周期性地基于Stream中各个Operator的状态来生成Snapshot,从而将这些状态数据定 ...
- Nuxt.js 应用中的 schema:resolved 事件钩子详解
title: Nuxt.js 应用中的 schema:resolved 事件钩子详解 date: 2024/11/13 updated: 2024/11/13 author: cmdragon exc ...
- thinkpad x250装manjaro linux,解决指纹和远程桌面问题
前言 家里有个thinkpad x250闲置,平时主要用windows,大概隔一年半年装一次linux看看发展程度. 自己平时用的服务器一般装centos和debian,偶尔是ubuntu. 桌面li ...
- MySQL-8.3.0 innovation 创新版本YUM安装配置
MySQL-8.3.0 innovation版本已发布了,想抢先体验一下最新的功能,可以用以下的方式快速在虚拟机上安装一下哈 服务器环境:[root@node213 ~]# cat /etc/redh ...
- linux学习用到的命令
创建快件方式 ln 创建目录的快件方式 sudo ln -s /root/myhack/ /root/Desktop以上指令是创建软链接到桌面. ln -s /mnt/hgfs/VMware_shar ...
- MySQL数据库设计规范(新)
目录 规范背景与目的 设计规范2.1 数据库设计2.1.1 库名2.1.2 表结构2.1.3 列数据类型优化2.1.4 索引设计2.1.5 分库分表.分区表2.1.6 字符集2.1.7 程序DAO层设 ...
- Vim之PHP语法检查
在Linux下操作,一般都是使用vim进行文本编辑, 这个时候有可能不小心就会出现语法异常,导致程序错误 手动检查: 1) 编辑完成之后, 回到命令行下执行 php -l test.php 如果语法校 ...
- geocodeCN:一个批量将地址转为地理坐标的插件
目录 1. 介绍 2. 使用步骤: 2.1 安装 2.2 配置 2.3 坐标匹配 2.4 生成图层 2.5 导出为CSV 3. 说明 1. 介绍 这是一个QGIS插件,主要用于批量地理编码,即将地址转 ...
- AspNetCore全局异常处理
在开发ASP.NET Core应用程序时,全局异常处理是一个重要的概念.它允许我们集中处理应用程序中未捕获的异常,确保应用程序的稳定性和用户体验. 1. 为什么需要全局异常处理 全局异常处理的目的是为 ...