当前位置:网站首页>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) {
}
}
边栏推荐
猜你喜欢

How to make ERP inventory accounts of chemical enterprises more accurate

Analyze the knowledge transfer and sharing spirit of maker Education

Interpreting the daily application functions of cooperative robots

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

MySQL deep paging optimization with tens of millions of data, and online failure is rejected!

第05章_存储引擎

【案例】定位的运用-淘宝轮播图

Cutting edge technology for cultivating robot education creativity

Pytorch实战——MNIST数据集手写数字识别

Influence of oscilloscope probe on signal source impedance
随机推荐
实现浏览页面时校验用户是否已经完成登录的功能
XML建模
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
判断横竖屏的最佳实现
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)
Prior knowledge of machine learning in probability theory (Part 1)
XML modeling
Pytoch practice -- MNIST dataset handwritten digit recognition
Is it necessary for bazel to learn
postgres 建立连接并删除记录
Learning notes of SAS programming and data mining business case 19
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
int GetMonth( ) const throw( );后面的throw( )什么意思?
概率论机器学习的先验知识(上)
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
示波器探头对测量带宽的影响
Sophomore personal development summary
Viewrootimpl and windowmanagerservice notes
@Validated basic parameter verification, grouping parameter verification and nested parameter verification