当前位置:网站首页>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) {
}
}
边栏推荐
- sql系列(基础)-第二章 限制和排序数据
- Web Service简单入门示例
- MySQL ifnull usage function
- Comparison table of foreign lead American abbreviations
- Is it necessary for bazel to learn
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- 2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
- 基于AVFoundation实现视频录制的两种方式
猜你喜欢
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
MySQL InnoDB架构原理
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
Five layer network protocol
The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
教你自己训练的pytorch模型转caffe(二)
Influence of oscilloscope probe on signal source impedance
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
随机推荐
Simple getting started example of Web Service
Hdu2377bus pass (build more complex diagram +spfa)
Implementation of redis unique ID generator
The development of research tourism practical education helps the development of cultural tourism industry
Is it necessary for bazel to learn
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
国外LEAD美国简称对照表
SQL series (basic) - Chapter 2 limiting and sorting data
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
PVC plastic sheets BS 476-6 determination of flame propagation properties
Learning notes of SAS programming and data mining business case 19
EN 438-7 laminated sheet products for building covering decoration - CE certification
MySQL ifnull usage function
模式-“里氏替换原则”
股票开户选择哪家证券公司比较好哪家平台更安全
Which is the best online collaboration product? Microsoft loop, notion, flowus
第05章_存储引擎
PHP deserialization +md5 collision
最长摆动序列[贪心练习]
Viewrootimpl and windowmanagerservice notes