当前位置:网站首页>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 InnoDB架构原理
- 100 cases of shell programming
- 从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
- Binary search
- Utils/index TS tool function
- [case] Application of element display and hiding -- element mask
- 使用WebAssembly在浏览器端操作Excel
- 10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
- 中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
- Viewrootimpl and windowmanagerservice notes
猜你喜欢

木板ISO 5660-1 热量释放速率摸底测试

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

Maker education infiltrating the transformation of maker spirit and culture

教你自己训练的pytorch模型转caffe(三)

学习机器人无从下手?带你体会当下机器人热门研究方向有哪些

Analyze the knowledge transfer and sharing spirit of maker Education

Clion configures Visual Studio (MSVC) and JOM multi-core compilation

使用WebAssembly在浏览器端操作Excel

leetcode:1755. 最接近目标值的子序列和

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
随机推荐
启牛2980有没有用?开户安全吗、
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
【案例】定位的运用-淘宝轮播图
Golang(1)|从环境准备到快速上手
股票开户选择哪家证券公司比较好哪家平台更安全
How to make ERP inventory accounts of chemical enterprises more accurate
终端安全能力验证环境搭建和渗透测试记录
Influence of oscilloscope probe on signal source impedance
Simple getting started example of Web Service
Material design component - use bottomsheet to show extended content (II)
sql系列(基础)-第二章 限制和排序数据
Learning robots have no way to start? Let me show you the current hot research directions of robots
Écrire une interface basée sur flask
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
实现浏览页面时校验用户是否已经完成登录的功能
珍爱网微服务底层框架演进从开源组件封装到自研
Realize the function of verifying whether the user has completed login when browsing the page
R language [data management]
leetcode:1139. The largest square bounded by 1
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)