当前位置:网站首页>easyexcel的使用
easyexcel的使用
2022-07-01 11:56:00 【qq_46198929】

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apche.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
package com.example.demo.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class DemoData {
//设置excel表头名称
@ExcelProperty(value = "学生编号",index = 0)
private Integer sno;
@ExcelProperty(value = "学生姓名",index = 1)
private String sname;
}
package com.example.demo.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.Map;
public class ExcelListener extends AnalysisEventListener<DemoData> {
//一行一行读取excel内容
@Override
public void invoke(DemoData data, AnalysisContext analysisContext) {
System.out.println("****"+data);
}
//读取表头内容
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头:"+headMap);
}
//读取完成之后
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
package com.example.demo.excel;
import com.alibaba.excel.EasyExcel;
import java.util.ArrayList;
import java.util.List;
public class TestEasyExcel {
public static void main(String[] args) {
//实现excel写的操作
//1 设置写入文件夹地址和excel文件名称
//String filename = "D:\\write.xlsx";
//2 调用easyexcel里面的方法实现写操作
//write方法两个参数:第一个参数文件路径名称,第二个参数实体类class
//EasyExcel.write(filename,DemoData.class).sheet("学生列表").doWrite(getData());
//实现excel读操作
String filename = "D:\\write.xlsx";
EasyExcel.read(filename,DemoData.class,new ExcelListener()).sheet().doRead();
}
//创建方法返回list集合
private static List<DemoData> getData() {
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setSno(i);
data.setSname("lucy"+i);
list.add(data);
}
return list;
}
}
边栏推荐
- Adjacency matrix undirected graph (I) - basic concepts and C language
- 2022/6/28学习总结
- The Missing Semester
- S7-1500plc simulation
- TMUX usage
- Value/hush in redis
- Force button homepage introduction animation
- openinstall:微信小程序跳转H5配置业务域名教程
- 迅为i.MX8Mmini开发板离线构建Yocto系统
- 用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义
猜你喜欢

The Missing Semester

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

Mechanism and type of CPU context switch

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Exposure: a white box photo post processing framework reading notes

Redis' attack tactics

深入理解 grpc part1

How to set decimal places in CAD

2022/6/28学习总结

ACLY与代谢性疾病
随机推荐
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
Golang根据参数引入相应配置文件的实现方式
C summary of knowledge points 1
Custom grpc plug-in
Mechanism and type of CPU context switch
2022/6/28学习总结
ES6 promise Usage Summary
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
epoll介绍
Golang introduces the implementation method of the corresponding configuration file according to the parameters
Summary of JFrame knowledge points 2
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
邻接矩阵无向图(一) - 基本概念与C语言
S7-1500PLC仿真
Talk about the pessimistic strategy that triggers full GC?
【单片机】【数码管】数码管显示
Binary stack (I) - principle and C implementation
2022/6/30学习总结
CAD如何設置標注小數比特
自定义 grpc 插件