当前位置:网站首页>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) {
}
}
边栏推荐
猜你喜欢
MySQL 千万数据量深分页优化, 拒绝线上故障!
When steam education enters personalized information technology courses
MySQL fully parses json/ arrays
Write an interface based on flask
基于flask写一个接口
【案例】定位的运用-淘宝轮播图
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
基于AVFoundation实现视频录制的两种方式
How to make ERP inventory accounts of chemical enterprises more accurate
教你自己训练的pytorch模型转caffe(二)
随机推荐
LeetCode: Distinct Subsequences [115]
Open source SPL eliminates tens of thousands of database intermediate tables
Introduction of ArcGIS grid resampling method
MySQL InnoDB架构原理
PVC plastic sheets BS 476-6 determination of flame propagation properties
木板ISO 5660-1 热量释放速率摸底测试
[case] Application of positioning - Taobao rotation map
Monorepo management methodology and dependency security
使用WebAssembly在浏览器端操作Excel
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Influence of oscilloscope probe on signal source impedance
postgres 建立连接并删除记录
浅聊我和一些编程语言的缘分
selenium 获取dom内属性值的方法
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
vant 源码解析之 utils/index.ts 工具函数
教你自己训练的pytorch模型转caffe(三)
Five layer network protocol