当前位置:网站首页>EasyExcel的读写操作
EasyExcel的读写操作
2022-07-05 21:05:00 【letg】
一、写操作
1.1 实体类
读操作添加 @ExcelProperty注解,并且value值为excel的表头名称
@Data
public class User {
@ExcelProperty(value = "用户编号",index = 0)
private int id;
@ExcelProperty(value = "用户名称",index = 1)
private String name;
}
1.2 测试写操作
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
EasyExcel.write(fileName,User.class)
.sheet("写操作")
.doWrite(data());
}
}
private static List<User> data(){
ArrayList<User> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(i);
user.setName("张三"+i);
list.add(user);
}
return list;
}
}
二、写操作(写操作需要继承一个监听器,进行一行一行读取)
2.1 监听器
invoke方法会进行excel一行一行读取,只会从第二行读取,第一行为表头
public class ExcelListener extends AnalysisEventListener<User> {
//一行一行读取excel的内容,把每行内容封装
//第一行不读,因为第一行为表头
@Override
public void invoke(User user, AnalysisContext analysisContext) {
System.out.println(user);
}
//表头是从该方法读取
@Override
public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
System.out.println("表头:"+headMap);
super.invokeHead(headMap, context);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
2.2 测试代码
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
// EasyExcel.write(fileName,User.class)
// .sheet("写操作")
// .doWrite(data());
EasyExcel.read(fileName,User.class,new ExcelListener())
.sheet("读操作")
.doRead();
}
三、实现
3.1 导出
public void exportData(HttpServletResponse response) {
try {
//设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("课程分类","UTF-8");
response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
//查询所有课程数据
List<Subject> subjectList = baseMapper.selectList(null);
List<SubjectEeVo> subjectEeVos = new ArrayList<>();
for (Subject subject : subjectList) {
SubjectEeVo subjectEeVo = new SubjectEeVo();
BeanUtils.copyProperties(subject,subjectEeVo);
subjectEeVos.add(subjectEeVo);
}
EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
.sheet("课程分类")
.doWrite(subjectEeVos);
} catch (Exception e) {
e.printStackTrace();
}
}
3.2 导入
@Override
public void importData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListener)
.sheet()
.doRead();
} catch (IOException e) {
throw new GgktException(20001,"导入失败");
}
}
@Component
public class SubjectListener extends AnalysisEventListener<SubjectEeVo> {
@Autowired
private SubjectMapper subjectMapper;
@Override
public void invoke(SubjectEeVo subjectEeVo, AnalysisContext analysisContext) {
Subject subject = new Subject();
BeanUtils.copyProperties(subjectEeVo,subject);
subjectMapper.insert(subject);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
边栏推荐
- Hdu2377bus pass (build more complex diagram +spfa)
- ODPs next map / reduce preparation
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- POJ 3414 pots (bfs+ clues)
- leetcode:1755. 最接近目标值的子序列和
- postgres 建立连接并删除记录
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- 大二下个人发展小结
- php中explode函数存在的陷阱
- [case] Application of element display and hiding -- element mask
猜你喜欢
Influence of oscilloscope probe on measurement bandwidth
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
How to send samples when applying for BS 476-7 display? Is it the same as the display??
XML建模
Duchefa cytokinin dihydrozeatin (DHZ) instructions
示波器探头对信号源阻抗的影响
请查收.NET MAUI 的最新学习资源
EN 438-7 laminated sheet products for building covering decoration - CE certification
Using webassembly to operate excel on the browser side
Use of thread pool
随机推荐
Monorepo management methodology and dependency security
vant 源码解析之 utils/index.ts 工具函数
Introduction of ArcGIS grid resampling method
Pytoch practice -- MNIST dataset handwritten digit recognition
Wood board ISO 5660-1 heat release rate mapping test
poj 3414 Pots (bfs+线索)
【案例】元素的显示与隐藏的运用--元素遮罩
终端安全能力验证环境搭建和渗透测试记录
Popular science | does poor English affect the NPDP exam?
《SAS编程和数据挖掘商业案例》学习笔记# 19
第05章_存储引擎
Sequence alignment
XML建模
Écrire une interface basée sur flask
selenium 查找b或p标签的内容
珍爱网微服务底层框架演进从开源组件封装到自研
Learning notes of SAS programming and data mining business case 19
Simple getting started example of Web Service
教你自己训练的pytorch模型转caffe(三)
Deep merge object deep copy of vant source code parsing