当前位置:网站首页>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) {
}
}
边栏推荐
- shell编程100例
- Dictionary tree simple introductory question (actually blue question?)
- vant 源码解析之 utils/index.ts 工具函数
- 学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
- Learning notes of SAS programming and data mining business case 19
- 判断横竖屏的最佳实现
- Clickhouse copy paste multi line SQL statement error
- 五层网络协议
- 终端安全能力验证环境搭建和渗透测试记录
- selenium 获取dom内属性值的方法
猜你喜欢
【案例】定位的运用-淘宝轮播图
浅聊我和一些编程语言的缘分
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
XML modeling
Influence of oscilloscope probe on signal source impedance
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
Interpreting the daily application functions of cooperative robots
ArcGIS栅格重采样方法介绍
最长摆动序列[贪心练习]
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
随机推荐
Hdu2377bus pass (build more complex diagram +spfa)
Viewrootimpl and windowmanagerservice notes
Maker education infiltrating the transformation of maker spirit and culture
100 cases of shell programming
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
ViewRootImpl和WindowManagerService笔记
Deep merge object deep copy of vant source code parsing
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
When steam education enters personalized information technology courses
Influence of oscilloscope probe on measurement bandwidth
水泥胶黏剂BS 476-4 不燃性测试
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
Web Service简单入门示例
ts 之 属性的修饰符public、private、protect
leetcode:1755. 最接近目标值的子序列和
Clion configures Visual Studio (MSVC) and JOM multi-core compilation
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
Test of incombustibility of cement adhesives BS 476-4
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
PVC plastic sheets BS 476-6 determination of flame propagation properties