当前位置:网站首页>Reading and writing operations of easyexcel
Reading and writing operations of easyexcel
2022-07-05 21:05:00 【letg】
One 、 Write operations
1.1 Entity class
Read operation add @ExcelProperty annotation , also value The value is excel Header name of
@Data
public class User {
@ExcelProperty(value = " The user id ",index = 0)
private int id;
@ExcelProperty(value = " User name ",index = 1)
private String name;
}
1.2 Test write operations
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
EasyExcel.write(fileName,User.class)
.sheet(" Write operations ")
.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(" Zhang San "+i);
list.add(user);
}
return list;
}
}
Two 、 Write operations ( The write operation needs to inherit a listener , Read line by line )
2.1 Monitor
invoke The method will carry out excel Read line by line , Only the second line will be read , The first line is the header
public class ExcelListener extends AnalysisEventListener<User> {
// Read line by line excel The content of , Encapsulate each line of content
// Don't read the first line , Because the first line is header
@Override
public void invoke(User user, AnalysisContext analysisContext) {
System.out.println(user);
}
// The header is read from this method
@Override
public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
System.out.println(" Header :"+headMap);
super.invokeHead(headMap, context);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
2.2 Test code
public class TestWrite {
public static void main(String[] args) {
String fileName = "C:\\data\\hzxy.xlsx";
// EasyExcel.write(fileName,User.class)
// .sheet(" Write operations ")
// .doWrite(data());
EasyExcel.read(fileName,User.class,new ExcelListener())
.sheet(" Read operations ")
.doRead();
}
3、 ... and 、 Realization
3.1 export
public void exportData(HttpServletResponse response) {
try {
// Set download information
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(" Course classification ","UTF-8");
response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
// Query all course data
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(" Course classification ")
.doWrite(subjectEeVos);
} catch (Exception e) {
e.printStackTrace();
}
}
3.2 Import
@Override
public void importData(MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListener)
.sheet()
.doRead();
} catch (IOException e) {
throw new GgktException(20001," Import failed ");
}
}
@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) {
}
}
边栏推荐
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- Material design component - use bottomsheet to show extended content (II)
- 树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
- 判断横竖屏的最佳实现
- Determine the best implementation of horizontal and vertical screens
- 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
- sql系列(基础)-第二章 限制和排序数据
- Utils/index TS tool function
- 字典树简单入门题(居然是蓝题?)
- LeetCode_哈希表_困难_149. 直线上最多的点数
猜你喜欢
leetcode:1139. 最大的以 1 为边界的正方形
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
Using webassembly to operate excel on the browser side
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
[case] Application of positioning - Taobao rotation map
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
基于AVFoundation实现视频录制的两种方式
CLion配置visual studio(msvc)和JOM多核编译
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
Write an interface based on flask
随机推荐
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
Comparison table of foreign lead American abbreviations
shell编程100例
大二下个人发展小结
股票开户选择哪家证券公司比较好哪家平台更安全
leetcode:1139. 最大的以 1 为边界的正方形
教你自己训练的pytorch模型转caffe(二)
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
浅聊我和一些编程语言的缘分
Golang(1)|从环境准备到快速上手
Pytoch practice -- MNIST dataset handwritten digit recognition
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
ts 之 属性的修饰符public、private、protect
MySQL ifnull usage function
Five layer network protocol
Clear app data and get Icon
Analyze the knowledge transfer and sharing spirit of maker Education
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
基于AVFoundation实现视频录制的两种方式
Learning robots have no way to start? Let me show you the current hot research directions of robots