SpringBoot graphic tutorials 10 – Excel template export | millions of data export | image export “easypoi”
December 25, 2023
by Cheryl Flores
No Comments
There is the concept of flying in the sky, there must be the realization of landing
The concept ten times is not as good as the code once, my friend, I hope you can type all the code cases in the article once
Like it before you look at it, make it a habit
SpringBoot illustrated tutorial series article directory
SpringBoot图文教程1 “concept + case mind mapping” “basics”
Use “logback” and “log4j” to log.
SpringBoot graphic tutorial 3 – “‘ first love ‘complex” integrated Jsp
4 – SpringBoot implementation file upload and download
Use Aop in SpringBoot
SpringBoot Tutorial 6 – The use of filters in SpringBoot
SpringBoot tutorial 7 – The use of SpringBoot interceptor posture is available here
SpringBoot integration with MBG code Generator
SpringBoot import/export Excel “Apache Poi”
preface
The last article briefly introduced the use of Poi, but using Poi to write code is a bit tedious, if you want to achieve complex requirements, such as: image export, multi-table data export, template export, large amount of data export, etc., using the most native Poi is not a good choice.
Should we wrap our own utility classes?
As good as the wheel is, it better be written by someone else. So from this article to introduce two excellent Poi tools Easypoi and Ali open source EasyExcel.
EasyPoi
EasyPoi is a library of tools that encapsulates POI and encapsulates some common Excel operations
Basic import and export
Image import and export
Import and export of multi-table data
Import and export of mass data
Exporting templates
Next we will work together to achieve the above functions
IO /#category_5… easyPoi.mydoc. IO /#category_5…
Basic import and export
In the most basic import/export, the entity class of the data to be exported is as follows:
publicclassTeacher {
/ * *
* The teacher's primary key
* /
private Integer teacherId;
/ * *
* name
* /
private String teacherName;
/ * *
* Address of profile picture
* /
private String teacherImage;
/ * *
* The teacher's status 0 means normal and 1 means deleted
* /
private Integer teacherStatus;
}
Copy the code
Omit the get the set
1. Import dependencies
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
Copy the code
2. Annotate the data entity class to be exported
The entity class of the data we want to export is Teacher, so we need to add Easypoi annotations to Teacher. Easypoi is annotate development, all the details of annotations are in the official document, but if the requirement to complete is the simplest import and export, @excel a annotation is enough.
@excel this is a mandatory annotation, if the need is simple, only use this one annotation is also ok, covers the common Excel requirements, you need to be familiar with this function, mainly divided into basic, image processing, time processing, merge processing several blocks
@Excel official API easypoi.mydoc. IO /#text_18690…
Well annotated entity classes are as follows:
3. Import and export directly using the utility classes in EasyPoi
EasyPoi is annotated development, and all Excel definitions, from styles to date formatting, are defined in the annotations of the entity class
Export code
/ * *
* easypoi export
* /
@Test
publicvoidtest4(a) throws IOException {
// Simulate data
List<Teacher> list = new ArrayList<>();
list.add(new Teacher(1."Miss Li"."hhh.jpg".1));
list.add(new Teacher(2."Miss Li"."hhh.jpg".1));
list.add(new Teacher(3."Miss Li"."hhh.jpg".1));
list.add(new Teacher(4."Miss Li"."hhh.jpg".1));
list.add(new Teacher(5."Miss Li"."hhh.jpg".1));
list.add(new Teacher(6."Miss Li"."hhh.jpg".1));
/ * *
* Export the parameter object
* Parameter 1 title
* Parameter 2 Specifies the name of the table
* /
ExportParams exportParams = new ExportParams("All teacher data"."teacher");
/ * *
* exportExcel Exports Excel files
* Parameter 1 Exports the parameter object
Parameter 2 Specifies the class object of the entity class to export
* Parameter 3 The data to be exported requires a collection of teacher objects queried from the collection database
*
* The return value is the encapsulated file object
About millions of data export recommended using Ali open source EasyExcel official introduction can control the memory in KB
Big data export means that when we export tens of thousands or millions of data, we query so much data from the database and load it into the memory at a time, and then write it into the memory, which will cause pressure on our memory and CPU. At this time, we need to handle the export like paging, and write into Excel in sections to relieve the pressure
EasyPoi provides two methods to force the use of the XSSF version of Excel
Database queries using MybatisPlus if you are interested in my related article SpringBoot integration with MybatisPlus
CSV and SXSSF are not as good as XSSF for millions of data. (POI provides a class for exporting large data.)
The template export
Template is a simple way to deal with complex Excel. Complex Excel styles can be directly edited with Excel, perfectly avoiding the minefield of code writing styles. At the same time, the support of instructions also improves the effectiveness of templates. EasyPoi support instructions and functions
A whitespace-delimited
Ternary operation {{test ? obj:obj2}}
N: indicates that the cell is a numeric type {{n:}}
FileOutputStream fos = new FileOutputStream("/Users/ K /Desktop/ Special expenditure application 111_map.xls");
workbook.write(fos);
fos.close();
}
Copy the code
The results are as follows
conclusion
Tips: Almost all Excel related work can be done with EasyPoi
You can follow the related articles POI and EasyExcel as well as the POI Chinese API document “40 postures for operating Excel Files”.
Congratulations on completing this chapter. A round of applause! If this article is helpful to you, please help to like, comment, retweet, this is very important to the author, thank you.
Let’s review the learning objectives of this article again
Master the use of Easypoi in SpringBoot
To learn more about SpringBoot, stay tuned for this series of tutorials.
Ask for attention, ask for likes, ask for retweets
Welcome to pay attention to my public account: Teacher Lu’s Java notes, will update Java technology graphic tutorials and video tutorials in the long term, Java learning experience, Java interview experience and Java actual combat development experience.