当前位置:网站首页>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) {
}
}
边栏推荐
- XML建模
- Web Service简单入门示例
- Material design component - use bottomsheet to show extended content (II)
- Five layer network protocol
- Interpreting the daily application functions of cooperative robots
- LeetCode: Distinct Subsequences [115]
- 基于AVFoundation实现视频录制的两种方式
- 从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
- 显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
- Pytorch实战——MNIST数据集手写数字识别
猜你喜欢

Talk about my fate with some programming languages

Influence of oscilloscope probe on signal source impedance

LeetCode_哈希表_困难_149. 直线上最多的点数

基于AVFoundation实现视频录制的两种方式

Clickhouse copy paste multi line SQL statement error

2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once

leetcode:1139. 最大的以 1 为边界的正方形

Learning robots have no way to start? Let me show you the current hot research directions of robots

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

教你自己训练的pytorch模型转caffe(二)
随机推荐
Aitm 2-0003 horizontal combustion test
示波器探头对信号源阻抗的影响
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
股票开户选择哪家证券公司比较好哪家平台更安全
Modifiers of attributes of TS public, private, protect
珍爱网微服务底层框架演进从开源组件封装到自研
模式-“里氏替换原则”
教你自己训练的pytorch模型转caffe(一)
R language [data management]
EN 438-7 laminated sheet products for building covering decoration - CE certification
木板ISO 5660-1 热量释放速率摸底测试
Display DIN 4102-1 Class B1 fire test requirements
Clickhouse copy paste multi line SQL statement error
获取前一天的js(时间戳转换)
Clear app data and get Icon
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
ts 之 属性的修饰符public、private、protect
100 cases of shell programming
驱动壳美国测试UL 2043 符合要求有哪些?