当前位置:网站首页>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) {
}
}
边栏推荐
- Learning robots have no way to start? Let me show you the current hot research directions of robots
- Introduction to TS, constructor and its this, inheritance, abstract class and interface
- 教你自己训练的pytorch模型转caffe(一)
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- Pytorch实战——MNIST数据集手写数字识别
- Comparison table of foreign lead American abbreviations
- Material design component - use bottomsheet to show extended content (II)
- ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
- Influence of oscilloscope probe on measurement bandwidth
- Interpreting the daily application functions of cooperative robots
猜你喜欢
Display DIN 4102-1 Class B1 fire test requirements
显示屏DIN 4102-1 Class B1防火测试要求
R语言【数据管理】
Add ICO icon to clion MinGW compiled EXE file
ClickHouse 复制粘贴多行sql语句报错
XML modeling
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
浅聊我和一些编程语言的缘分
基於flask寫一個接口
R language [data management]
随机推荐
Learning notes of SAS programming and data mining business case 19
Traps in the explode function in PHP
Material design component - use bottomsheet to show extended content (II)
SQL series (basic) - Chapter 2 limiting and sorting data
启牛2980有没有用?开户安全吗、
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
XML modeling
Cutting edge technology for cultivating robot education creativity
基於flask寫一個接口
Prior knowledge of machine learning in probability theory (Part 1)
Enclosed please find. Net Maui's latest learning resources
SYSTEMd resolved enable debug log
selenium 查找b或p标签的内容
Introduction of ArcGIS grid resampling method
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
Pytoch practice -- MNIST dataset handwritten digit recognition
Careercup its 1.8 serial shift includes problems
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Clear app data and get Icon
Wood board ISO 5660-1 heat release rate mapping test