当前位置:网站首页>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) {
}
}
边栏推荐
- 模式-“里氏替换原则”
- 请查收.NET MAUI 的最新学习资源
- 大二下个人发展小结
- Using webassembly to operate excel on the browser side
- Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
- EN 438-7 laminated sheet products for building covering decoration - CE certification
- CLion配置visual studio(msvc)和JOM多核编译
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- Écrire une interface basée sur flask
- Introduction to TS, constructor and its this, inheritance, abstract class and interface
猜你喜欢

Display DIN 4102-1 Class B1 fire test requirements

显示屏DIN 4102-1 Class B1防火测试要求

基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session

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

XML modeling

Use of thread pool

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

Talk about my fate with some programming languages

Golang(1)|从环境准备到快速上手

Five layer network protocol
随机推荐
Cutting edge technology for cultivating robot education creativity
shell编程100例
Get JS of the previous day (timestamp conversion)
LeetCode_哈希表_困难_149. 直线上最多的点数
Clear app data and get Icon
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
Introduction to TS, constructor and its this, inheritance, abstract class and interface
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
leetcode:1755. 最接近目标值的子序列和
MySQL 千万数据量深分页优化, 拒绝线上故障!
显示屏DIN 4102-1 Class B1防火测试要求
股票开户选择哪家证券公司比较好哪家平台更安全
Mode - "Richter replacement principle"
100 cases of shell programming
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
Viewrootimpl and windowmanagerservice notes
Use of thread pool
第05章_存储引擎
Wood board ISO 5660-1 heat release rate mapping test
The reason why the ncnn converted model on raspberry pie 4B always crashes when called