当前位置:网站首页>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) {
}
}
边栏推荐
- Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
- 最长摆动序列[贪心练习]
- Is it necessary for bazel to learn
- 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
- 大二下个人发展小结
- Introduction to TS, constructor and its this, inheritance, abstract class and interface
- 从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- ts 之 属性的修饰符public、private、protect
- 驱动壳美国测试UL 2043 符合要求有哪些?
猜你喜欢

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel

haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)

Wood board ISO 5660-1 heat release rate mapping test

When steam education enters personalized information technology courses

MySQL 千万数据量深分页优化, 拒绝线上故障!

Five layer network protocol

LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line

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

显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
随机推荐
hdu2377Bus Pass(构建更复杂的图+spfa)
Is Kai Niu 2980 useful? Is it safe to open an account
Is it necessary for bazel to learn
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Clion-MinGW编译后的exe文件添加ico图标
LeetCode: Distinct Subsequences [115]
Learning notes of SAS programming and data mining business case 19
ViewRootImpl和WindowManagerService笔记
基於flask寫一個接口
Influence of oscilloscope probe on signal source impedance
Clear app data and get Icon
启牛2980有没有用?开户安全吗、
ts 之 属性的修饰符public、private、protect
int GetMonth( ) const throw( );后面的throw( )什么意思?
Using webassembly to operate excel on the browser side
珍爱网微服务底层框架演进从开源组件封装到自研
【案例】定位的运用-淘宝轮播图
ClickHouse 复制粘贴多行sql语句报错
Clickhouse copy paste multi line SQL statement error