当前位置:网站首页>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();
}
}
}
边栏推荐
- 企业架构与项目管理的关联和区别
- GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
- 100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
- [ecological partner] Kunpeng system engineer training
- Why does blocprovider feel similar to provider?
- 【JetCache】JetCache的使用方法与步骤
- Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
- leetcode - 287. 寻找重复数
- 【MySQL】索引的分类
- MIT|256KB 内存下的设备上训练
猜你喜欢
MySQL learning notes - SQL optimization of optimization
Business visualization - make your flowchart'run'up
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
并发编程系列之FutureTask源码学习笔记
Mysql——》MyISAM存储引擎的索引
Ida dynamic debugging apk
Pytorch sharpening chapter | argmax and argmin functions
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
三翼鸟两周年:羽翼渐丰,腾飞指日可待
I received a letter from CTO inviting me to interview machine learning engineer
随机推荐
Basic operation of binary tree
MySQL之MHA高可用配置及故障切换
Introduction and download of the latest version of airserver2022
Can you get a raise? Analysis on gold content of PMP certificate
Communication between browser tab pages
Spark interview questions
Copy ‘XXXX‘ to effectively final temp variable
Count the number of each character in the character
【MySQL】数据库优化方法
Use of vscode
2020-ViT ICLR
为什么数字化转型战略必须包括持续测试?
QT版本华睿相机的Demo程序实现
flink sql-client 使用 对照并熟悉官方文档
【单体】流辰信息I-BPSv3服务器推荐配置
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
Unity uses SQLite
Mask wearing detection method based on yolov5
Little p weekly Vol.11
K-means based user portrait clustering model