当前位置:网站首页>EasyExcel:读取Excel数据到List集合中
EasyExcel:读取Excel数据到List集合中
2022-06-27 06:03:00 【YUELEI118】
1. pom.xml
添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
2. 定义实体类
注解:@ExcelProperty("参数"),参数对应Excel中的列名,没有顺序,想取哪一列就写哪一列。
public class ReportDetail {
// ExcelIgnore 注解:实体类需要,但是Excel中没有的列,这里的id是数据库的自增ID主键
@ExcelIgnore
private Integer id;
@ExcelProperty("姓名")//Excel中的列名
private String personName;
.
.
// 无参构造 :必须
// 有参构造,get/set方法
3. 定义存放Excel中数据的list集合
ArrayList<ReportDetail> reportDetails = new ArrayList<>();
4. 读取Excel
read方法中的参数:
第一个参数:path:Excel文件的路径或者InputStream
第二个参数:实体类
第三个参数:ReadListener的匿名实现类,泛型中的参数:实体类类型
EasyExcel.read(路径, 实体类类名.class, new AnalysisEventListener<实体类类名>() {
@Override
public void invoke(实体类类名 形参, AnalysisContext analysisContext) {
list集合.add(形参);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet("Excel中的表名,默认第一个表").doRead();
- 对应上面的实体类和list集合
EasyExcel.read(path, ReportDetail.class, new AnalysisEventListener<ReportDetail>() {
@Override
public void invoke(ReportDetail reportExcel, AnalysisContext analysisContext) {
// 将读取到的每一行存入reportDetails集合中
reportDetails.add(reportExcel);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet().doRead();
边栏推荐
- Change the status to the corresponding text during MySQL query
- openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
- Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
- The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
- Built in functions of spark
- Win 10 如何打开环境变量窗口
- Netease cloud music params and encseckey parameter generation code
- 【QT小记】QT中正则表达式QRegularExpression的基本使用
- 427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
- MATLAB快速将影像的二维坐标转换为经纬度坐标
猜你喜欢

爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)

G1 and ZGC garbage collector
![Senior [Software Test Engineer] learning route and necessary knowledge points](/img/51/1be2e0812a6bca9e5e8d14bf254254.png)
Senior [Software Test Engineer] learning route and necessary knowledge points

Program ape learning Tiktok short video production

Run opcua protocol demo on raspberry pie 4B to access kubeedge

汇编语言-王爽 第9章 转移指令的原理-笔记

KubeSphere 集群配置 NFS 存储解决方案-收藏版

How to check the frequency of memory and the number of memory slots in CPU-Z?

JVM对象组成和存储

汇编语言-王爽 第11章 标志寄存器-笔记
随机推荐
多线程基础部分Part3
My opinion on test team construction
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
JVM overall structure analysis
免费的 SSH 和 Telnet 客户端PuTTY
【养成系】常用正则表达式
JVM tuning ideas
JVM类加载机制
310. 最小高度树
函数式 连续式
Two position relay xjls-8g/220
Proxy-Reflect使用详解
What's new in redis4.0 - active memory defragmentation
Two position relay rxmvb2 r251 204 110dc
【QT小点】QT下载链接
Senior [Software Test Engineer] learning route and necessary knowledge points
汇编语言-王爽 第8章 数据处理的两个基本问题-笔记
使用CSDN 开发云搭建导航网站
Formation and release of function stack frame
Configuring the help class iconfiguration in C # NETCORE