当前位置:网站首页>Easyexcel configuration and Application
Easyexcel configuration and Application
2022-06-11 13:37:00 【QLUGCL】
List of articles
Excel Import and export application scenarios
1 、 Data import : Reduce the input workload
2 、 Export data : Statistical information archiving
3 、 The data transfer : Data transmission between heterogeneous systems
EasyExcel characteristic
- Java Domain analysis 、 Generate Excel Well known frameworks are Apache poi、jxl etc. . But they all have a serious problem that is very memory consuming . If your system has a small amount of concurrency, it may be OK , But once concurrency comes up, it will OOM perhaps JVM Frequent full gc.
- EasyExcel Alibaba is an open source excel Processing framework , To use the Simple 、 Save memory Is famous for its .EasyExcel The main reason that can greatly reduce the memory occupation is parsing Excel The file data is not loaded into memory at one time , Instead, read data from a row on the disk , One by one .
- EasyExcel Line by line parsing mode ( read ), And the parsing results of one line are notified and processed in the observer mode (AnalysisEventListener)
EayExcel The bottom layer of the is to interact directly with the disk , And read the table row by row , Memory usage is very small , The test results are shown below .
EasyExcel To configure
pom Introduction in xml Related dependencies
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
contrast excel Column attribute create entity class
Set the header and added data fields get,set Method
@Data
public class DemoData {
// Set up excel Header name ,index The first few columns
@ExcelProperty(value = " Student number ",index = 0)
private Integer sno;
// Set header name
@ExcelProperty(value = " The student's name ",index = 1)
private String sname;
}
Implement write operation ( Write data to excel)

// Realization excel The operation of writing
//1 Set the write folder address and excel File name
String filename = "F:\\write.xlsx";
//2 call easyexcel The method inside realizes the write operation
//write Method two parameters : First parameter file path name , Second parameter entity class .class= The path of the entity class ,sheet yes excel The subscript of the file , The file stream closes automatically
EasyExcel.write(filename,DemoData.class).sheet(" Student list ").doWrite(getData());
Implement read operation ( read out excel data )
- read excel operation , Mainly in the set listening class invoke Processing entity class parameters in the function , Realize the functions of reading data and processing data ( Use the incoming service Parameters are processed )
- Read excel Content , Read line by line excel Content , Do not read the header , And then use invoke Function implements line by line processing through entity class parameters ( if excel There are multiple lines of data ,invoke Will be called many times , Pass entity class parameters multiple times ( Store each line ))
- Because reading excel Monitor class AnalysisEventListener< Entity class > Can't give it to spring Conduct management ( Cannot use annotations ), Need yourself new, Cannot inject other objects , Unable to implement database operation , Because you can't inject service,mapper wait , Of course, you can also directly use the most primitive jdbc Way
- When instantiating the listening class, you will service Pass as a constructor parameter
new SubjectExcelListener(subjectService))
Operation code
try {
// File input stream
InputStream in = file.getInputStream();
// Call the method to read the incoming excel and service object
EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
}catch(Exception e){
e.printStackTrace();
}
Monitor class
package com.qlugcl.eduservice.listener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qlugcl.eduservice.entity.EduSubject;
import com.qlugcl.eduservice.entity.excel.SubjectData;
import com.qlugcl.eduservice.service.EduSubjectService;
import com.qlugcl.servicebase.exceptionhandler.GCLException;
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
// because SubjectExcelListener Can't give it to spring Conduct management ( Cannot use annotations ), Need yourself new, Cannot inject other objects
// Unable to implement database operation , Because you can't inject service,mapper wait , Structural method with parameters , Of course, you can also directly use the most primitive jdbc The way to
public EduSubjectService subjectService;
public SubjectExcelListener() {
}
// Here by passing service Parameters , So as to realize the operation of the database
public SubjectExcelListener(EduSubjectService subjectService) {
this.subjectService = subjectService;
}
// Read excel Content , Read line by line
@Override
public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
if(subjectData == null) {
throw new GCLException(20001," File data is empty ");
}
// Read line by line , There are two values per read , The first value is a primary classification , The second value is the secondary classification
// Judge whether the classification is repeated , Whether there is
EduSubject existOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
if(existOneSubject == null) {
// There is no same primary classification , Add
existOneSubject = new EduSubject();
existOneSubject.setParentId("0");
existOneSubject.setTitle(subjectData.getOneSubjectName());// First class classification name
subjectService.save(existOneSubject);
}
// Get the first level classification id value
String pid = existOneSubject.getId();
// Add secondary classification
// Judge whether the secondary classification is repeated , Whether there is
EduSubject existTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
if(existTwoSubject == null) {
existTwoSubject = new EduSubject();
existTwoSubject.setParentId(pid);
existTwoSubject.setTitle(subjectData.getTwoSubjectName());// Secondary classification name
subjectService.save(existTwoSubject);
}
}
// Judge that the primary classification cannot be added repeatedly
private EduSubject existOneSubject(EduSubjectService subjectService,String name) {
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title",name);
wrapper.eq("parent_id","0");
EduSubject oneSubject = subjectService.getOne(wrapper);
return oneSubject;
}
// Judge that the secondary classification cannot be added repeatedly
private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid) {
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title",name);
wrapper.eq("parent_id",pid);
EduSubject twoSubject = subjectService.getOne(wrapper);
return twoSubject;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
EasyExcel read excel application

read excel operation , Mainly in the set listening class invoke Function to read and process data
package com.createcode.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.Date;
import java.util.Map;
//excel Reading listener , Once a read is generated excel operation , Then the corresponding configuration for reading will be triggered
public class ExcelListener extends AnalysisEventListener<DemoData> {
// Read line by line excel Content , Do not read the header , stay invoke Function to operate on the read data
@Override
public void invoke(DemoData data, AnalysisContext analysisContext) {
System.out.println("****"+data+""+new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Read the contents of the header
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println(" Header :"+headMap);
}
// After the read is complete
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
summary
Read operations : read out excel The data is listening for the class invoke Function to process row by row entity class parameters and store them in the database
As shown in the figure :easyexcel Just handle excel Data interaction with the database , Format converted 


边栏推荐
- 二十八-三维点云实时和离线生成二维栅格、三维栅格地图
- In 2022, capture these 12 data and analyze trends!
- /usr/bin/gzip: 1: ELF: not found /usr/bin/gzip: 3: : not found /usr/bin/gzip: 4: Syntax erro
- 2022工具钳工(中级)操作证考试题库及答案
- 关于分布式锁的续命问题——基于Redis实现的分布式锁
- 2022 年,捕捉这 12 个数据和分析趋势!
- 利用 VSCode 的代码模板提高 MobX 的编码效率
- Terraform + Ansible实现基础设施及配置管理
- Pki/tls Swiss Army knife cfssl
- Will Apple build a search engine?
猜你喜欢

The global mobile phone market is declining, and even apple does not expect too much of the iphone14
![[filter] design of time-varying Wiener filter based on MATLAB [including Matlab source code 1870]](/img/1a/7b80f3d81c1f4773194cffa77fdfae.png)
[filter] design of time-varying Wiener filter based on MATLAB [including Matlab source code 1870]

Energy storage operation and configuration analysis of high proportion wind power system (realized by Matlab)

Code comparison tool, I use these six

Bs-xx-007 registered residence management system based on JSP

On the continuing Life of Distributed Locks - - Distributed Locks Based on redis

高比例风电电力系统储能运行及配置分析(Matlab实现)

Introduction to long connection

How to write high-performance code (IV) optimize data access
![[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers](/img/7f/8ffc83e5717275b27f1fd34111ca15.png)
[signal de-noising] chromatographic baseline estimation and de-noising based on sparsity (beads) with matlab code and papers
随机推荐
2022 年,捕捉这 12 个数据和分析趋势!
PKI/TLS瑞士军刀之cfssl
Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
XXVIII - 3D point cloud real-time and offline generation of 2D grid and 3D grid map
Ecplise cannot connect to SQL Server
微软再曝“丑闻”:在办公室看 VR 黄片,“HoloLens 之父”即将离职!
【信号去噪】基于稀疏性 (BEADS) 实现色谱基线估计和去噪附matlab代码和论文
tp6基于whoops的异常接管(漂亮的界面)
Introduction to long connection
JDBC connection pool is used for batch import. 5million data are run each time, but various problems will occur in the middle
2022工具钳工(中级)操作证考试题库及答案
Huawei HECs ECS is used to build a telegraf+influxdb+grafana monitoring system [Huawei cloud to jianzhiyuan]
应用编排Nomad与Kubernetes对比
折叠表达式
程序员到了35岁之后的一些转型思考
Hashicopy之nomad应用编排方案06(配置task)
二十八-三维点云实时和离线生成二维栅格、三维栅格地图
SQL:如何用采购单销售单的数据 通过移动加权平均法 计算商品成本
How to write high-performance code (IV) optimize data access
[the path of system analyst] collection of wrong topics of system analyst