当前位置:网站首页>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) {
}
}
边栏推荐
- Deep merge object deep copy of vant source code parsing
- 启牛2980有没有用?开户安全吗、
- Clion-MinGW编译后的exe文件添加ico图标
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Aitm2-0002 12s or 60s vertical combustion test
- Écrire une interface basée sur flask
- Viewrootimpl and windowmanagerservice notes
- 《SAS编程和数据挖掘商业案例》学习笔记# 19
- The reason why the ncnn converted model on raspberry pie 4B always crashes when called
- 教你自己训练的pytorch模型转caffe(三)
猜你喜欢
[case] Application of positioning - Taobao rotation map
【案例】定位的运用-淘宝轮播图
The development of research tourism practical education helps the development of cultural tourism industry
PVC 塑料片BS 476-6 火焰传播性能测定
珍爱网微服务底层框架演进从开源组件封装到自研
基于flask写一个接口
Five layer network protocol
基於flask寫一個接口
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Wood board ISO 5660-1 heat release rate mapping test
随机推荐
Monorepo management methodology and dependency security
100 cases of shell programming
Display DIN 4102-1 Class B1 fire test requirements
Dictionary tree simple introductory question (actually blue question?)
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
Cutting edge technology for cultivating robot education creativity
第05章_存储引擎
Maker education infiltrating the transformation of maker spirit and culture
示波器探头对测量带宽的影响
R语言【数据管理】
教你自己训练的pytorch模型转caffe(三)
ClickHouse 复制粘贴多行sql语句报错
Open source SPL eliminates tens of thousands of database intermediate tables
AITM 2-0003 水平燃烧试验
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Hdu2377bus pass (build more complex diagram +spfa)
CareerCup它1.8 串移包括问题
《SAS编程和数据挖掘商业案例》学习笔记# 19
Sophomore personal development summary
字典树简单入门题(居然是蓝题?)