当前位置:网站首页>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 empties table data
- Getting started with the lockust series
- 详解JMM
- Recent public ancestor offline practice (tarjan)
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
- 【JetCache】JetCache的使用方法与步骤
- 黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
- EasyExcel 复杂数据导出
- Redis配置与优化
- locust 系列入门
猜你喜欢
随机推荐
Recent public ancestor (LCA) online practices
YOLOv5.5 调用本地摄像头
[ecological partner] Kunpeng system engineer training
【MySQL】数据库优化方法
Icml2022 | interventional contrastive learning based on meta semantic regularization
【MySQL】explain的基本使用以及各列的作用
固定资产管理子系统报表分为什么大类,包括哪些科目
MySQL empties table data
小 P 周刊 Vol.11
Four methods of JS array splicing [easy to understand]
What is the difference between consonants and Initials? (difference between initials and consonants)
#yyds干货盘点# 解决名企真题:扭蛋机
Mask wearing detection method based on yolov5
配置筛选机
LC501. 二叉搜索树中的众数
Little p weekly Vol.11
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
PyTorch磨刀篇|argmax和argmin函数
Simple interactive operation of electron learning (III)
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p