当前位置:网站首页>Easyexcel complex data export
Easyexcel complex data export
2022-07-01 22:34:00 【The moon is drunk with the wind】
EasyExcel Realization Multi title 、 Dynamic title , And implement a Excel There are multiple tables sheet, One sheet There are multiple tables
- One 、 Write an entity class according to the document
- Two 、 Refer to the two methods written in the document , Namely ** title ** And ** data **
- 3、 ... and 、【 Multiple sheet】 reference EasyExcel The official website exports data in different sheet On
- Four 、【 same sheet Multiple tables 】
- 5、 ... and 、【Excel More than one of them sheet, And one sheet Multiple tables 】
Export data for work , Export to excel The format is as follows :
demand 1: Suppose there is A、B staff , Then it's equivalent to having two worksheets 【sheet】
demand 2: hypothesis A Employees should show their salary for one year , Want the same worksheet 【sheet】 You have to have multiple watches 
Then, first of all, dedicate EasyExcel Language sparrow perhaps EasyExcel Official website
The official website I first read , But the official website doesn't write as much as YuQue . But I can't stand the simplicity of the official website . Um. … I think it's simple
One 、 Write an entity class according to the document
package com.admin.ctt.entity.excelPojo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
/** * Export the contract Commission and performance of employees * @author LunarYouI * @create 2022-06-28 17:05 */
@Data
@EqualsAndHashCode
// The height of all titles
@HeadRowHeight(30)
// The height of all content
@ContentRowHeight(25)
// All cell column widths
@ColumnWidth(25)
public class EmployeeSalaryPojo {
// @ColumnWidth(50) // Specify a cell column width
// @ExcelProperty({" Employee payroll ", " title 1", " Contract royalty / The performance of ", " String title "})
// private String string;
//
// @ExcelProperty({" Employee payroll ", " title 1", " Contract royalty / The performance of ", " Date title "})
// private Date date;
//
// @ExcelProperty({" Employee payroll ", " title 1", " Contract royalty / The performance of ", " Digital title "})
// private Double doubleData;
// Employee name
private String name;
// Employee date
private Date date;
// Wages
private Double doubleData;
}
Two 、 Refer to the two methods written in the document , Namely title And data
- Here I define two people , They are Wang 、 Li Mou , Two for each sheet
/** * data * LunarYouI */
private static List<EmployeeSalaryPojo> data() {
List<EmployeeSalaryPojo> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
EmployeeSalaryPojo data = new EmployeeSalaryPojo();
data.setName(" Sb. " + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
/** * title * @return */
private static Map<String, List<List<String>>> head() {
String a = " Administrators : zhang Wage time :2022.01 Position : staff / Executor full name : Wang mou ";
String b = " Administrators : Li Mou Wage time :2022.01 Position : staff / Executor full name : Li Mou ";
Map<String, List<List<String>>> mp = new HashMap<>();
/** * people 1 */
List<List<String>> head = new ArrayList<>();
List<String> list1 = new ArrayList<>();
list1.add(" Employee payroll ");
list1.add(a);
list1.add(" Contract royalty / The performance of ");
list1.add(" Employee name ");
head.add(list1);
List<String> list2 = new ArrayList<>();
list2.add(" Employee payroll ");
list2.add(a);
list2.add(" Contract royalty / The performance of ");
list2.add(" Employee date ");
head.add(list2);
List<String> list3 = new ArrayList<>();
list3.add(" Employee payroll ");
list3.add(a);
list3.add(" Contract royalty / The performance of ");
list3.add(" Wages ");
head.add(list3);
mp.put("r1",head);
/** * people 2 */
List<List<String>> head1 = new ArrayList<>();
List<String> list4 = new ArrayList<>();
list4.add(" Employee payroll ");
list4.add(b);
list4.add(" Contract royalty / The performance of ");
list4.add(" Employee name ");
head1.add(list4);
List<String> list5 = new ArrayList<>();
list5.add(" Employee payroll ");
list5.add(b);
list5.add(" Contract royalty / The performance of ");
list5.add(" Employee date ");
head1.add(list5);
List<String> list6 = new ArrayList<>();
list6.add(" Employee payroll ");
list6.add(b);
list6.add(" Contract royalty / The performance of ");
list6.add(" Wages ");
head1.add(list6);
mp.put("r2",head1);
return mp;
}
3、 ... and 、【 Multiple sheet】 reference EasyExcel The official website exports data in different sheet On
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// Method 2: If you write different sheet Same object
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// here Specified file
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
// To call write , Here I call five times , The actual usage is based on the total number of pages in the database . This will eventually write 5 individual sheet Inside
for (int i = 0; i < 5; i++) {
// Create... Every time writeSheet Note here that you must specify sheetNo and sheetName It has to be different
WriteSheet writeSheet = EasyExcel.writerSheet(i, " Templates " + i).build();
// Write to excel Space a few lines with the top
writeSheet.setRelativeHeadRowIndex(1);
// Paging to the database to query data Here you can go to the database to query the data of each page
List<EmployeeSalaryPojo> data = data();
excelWriter.write(data, writeSheet);
}
} finally {
if(excelWriter!=null){
excelWriter.finish();
}
}
}
here No title Because EmployeeSalaryPojo Entity class The first three fields in are notes 了 , Untie the first three notes The last three are ok 
Four 、【 same sheet Multiple tables 】
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// Method 2: If you write different sheet Same object
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// here Specified file
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
// hold sheet Set to no header Otherwise it will output sheet The head of the So it looks like the first table There is 2 It's a head
WriteSheet writeSheet = EasyExcel.writerSheet(" Templates ").needHead(Boolean.FALSE).build();
// One line away from the top
writeSheet.setRelativeHeadRowIndex(10);
// You must specify a header here ,table Will inherit sheet Configuration of ,sheet The configuration does not require ,table By default, you don't need
WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
// The first write creates a header
excelWriter.write(data(), writeSheet, writeTable0);
// The second write will also create a header , Then write data after the first time
excelWriter.write(data(), writeSheet, writeTable1);
} finally {
if(excelWriter!=null){
excelWriter.finish();
}
}
}

5、 ... and 、【Excel More than one of them sheet, And one sheet Multiple tables 】
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// Method 2: If you write different sheet Same object
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// here Specified file
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
// Set dynamic title
Map<String, List<List<String>>> head = head();
int i = 0;
for (String key : head.keySet()) {
i++;
/** * 1、 Get value; Acquired List<List<String>> It means the same sheet The data of */
List<List<String>> lists = head.get(key);
// Create... Every time writeSheet Note here that you must specify sheetNo and sheetName It has to be different 【 We have to add .needHead(Boolean.FALSE), Otherwise, there will be many titles and no data 】
WriteSheet writeSheet = EasyExcel.writerSheet(i, " Templates " + i).needHead(Boolean.FALSE).build();
// Dynamic title
writeSheet.setHead(lists);
// Write to excel Space a few lines with the top
writeSheet.setRelativeHeadRowIndex(1);
/** * 2、 The same data is written in the same sheet Inside * there j Express 1 individual sheet There will be several form data */
for (int j = 0;j<3;j++) {
WriteTable writeTable = EasyExcel.writerTable(j).needHead(Boolean.TRUE).build();
excelWriter.write(data(), writeSheet, writeTable);
}
}
} finally {
if(excelWriter!=null){
excelWriter.finish();
}
}
}

边栏推荐
- Mysql——》索引存储模型推演
- QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
- 首席信息官对高绩效IT团队定义的探讨和分析
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- MySQL数据库详细学习教程
- 业务可视化-让你的流程图'Run'起来
- 【juc学习之路第8天】Condition
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Gaussdb (DWS) active prevention and troubleshooting
猜你喜欢
随机推荐
【MySQL】索引的分类
焱融看 | 混合云时代下,如何制定多云策略
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
并发编程系列之FutureTask源码学习笔记
使用闭包实现点击按钮切换 toggle
MySQL的视图练习题
配置筛选机
The difference between NiO and traditional IO
Spark interview questions
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Little p weekly Vol.11
记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
Basic knowledge of ngnix
Recent public ancestor (LCA) online practices
互联网的智算架构设计
Medium pen test questions: flip the string, such as ABCD, print out DCBA
Ida dynamic debugging apk
Make a three digit number of all daffodils "recommended collection"
Pytorch sharpening chapter | argmax and argmin functions




![[noip2013] building block competition [noip2018] road laying greed / difference](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)




