当前位置:网站首页>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) {
}
}
边栏推荐
- How to send samples when applying for BS 476-7 display? Is it the same as the display??
- SYSTEMd resolved enable debug log
- @Validated basic parameter verification, grouping parameter verification and nested parameter verification
- Wood board ISO 5660-1 heat release rate mapping test
- PVC 塑料片BS 476-6 火焰传播性能测定
- Sequence alignment
- postgis 安装地理信息扩展
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- Write an interface based on flask
- systemd-resolved 开启 debug 日志
猜你喜欢

PVC plastic sheets BS 476-6 determination of flame propagation properties

研學旅遊實踐教育的開展助力文旅產業發展

Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved

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

ClickHouse 复制粘贴多行sql语句报错

LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
![[case] Application of positioning - Taobao rotation map](/img/2d/c834ce95a2c8e53a20e67fa2e99439.png)
[case] Application of positioning - Taobao rotation map

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097

leetcode:1139. 最大的以 1 为边界的正方形

Add ICO icon to clion MinGW compiled EXE file
随机推荐
Establishment of terminal security capability verification environment and penetration test records
LeetCode_哈希表_困难_149. 直线上最多的点数
ClickHouse 复制粘贴多行sql语句报错
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
Clickhouse copy paste multi line SQL statement error
Traps in the explode function in PHP
【日常训练】729. 我的日程安排表 I
实现浏览页面时校验用户是否已经完成登录的功能
Material design component - use bottomsheet to show extended content (II)
显示屏DIN 4102-1 Class B1防火测试要求
Sophomore personal development summary
Mode - "Richter replacement principle"
序列联配Sequence Alignment
Comparison table of foreign lead American abbreviations
基於flask寫一個接口
Enclosed please find. Net Maui's latest learning resources
浅聊我和一些编程语言的缘分
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
LeetCode: Distinct Subsequences [115]
bazel是否有学习的必要