当前位置:网站首页>EasyExcel 复杂数据导出
EasyExcel 复杂数据导出
2022-07-01 21:43:00 【揽月随风醉】
EasyExcel实现 多标题、动态标题,并且实现一个Excel表有多个sheet,一个sheet有多个表
因工作需要导出数据,导出到excel的格式如下:
需求1:假设有A、B员工,那么就相当于有两张工作表【sheet】
需求2:假设A员工要展示一年的工资,要那么同一工作表【sheet】就得有多个表
再此先奉献上EasyExcel语雀 或者 EasyExcel官网
我最开始看的官网,但是官网又没有语雀写的多。但是耐不住官网的简洁。嗯…我觉得简洁
一、根据文档写了个实体类
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;
/** * 导出员工的合同提成及绩效 * @author LunarYouI * @create 2022-06-28 17:05 */
@Data
@EqualsAndHashCode
//所有标题的高
@HeadRowHeight(30)
//所有内容的高
@ContentRowHeight(25)
//所有单元格列宽
@ColumnWidth(25)
public class EmployeeSalaryPojo {
// @ColumnWidth(50) //指定某一个单元格列宽
// @ExcelProperty({"员工工资表", "标题1", "合同提成/绩效", "字符串标题"})
// private String string;
//
// @ExcelProperty({"员工工资表", "标题1", "合同提成/绩效", "日期标题"})
// private Date date;
//
// @ExcelProperty({"员工工资表", "标题1", "合同提成/绩效", "数字标题"})
// private Double doubleData;
//员工名称
private String name;
//员工日期
private Date date;
//员工工资
private Double doubleData;
}
二、参照文档写的两个方法,分别是标题与数据
- 标题这里我定义了两个人,分别是王某、李某,分别对应两个sheet
/** * 数据 * LunarYouI */
private static List<EmployeeSalaryPojo> data() {
List<EmployeeSalaryPojo> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
EmployeeSalaryPojo data = new EmployeeSalaryPojo();
data.setName("某人" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
/** * 标题 * @return */
private static Map<String, List<List<String>>> head() {
String a = "管理员:张某 工资时间:2022.01 职位:员工/执行人 姓名:王某";
String b = "管理员:李某 工资时间:2022.01 职位:员工/执行人 姓名:李某";
Map<String, List<List<String>>> mp = new HashMap<>();
/** * 人1 */
List<List<String>> head = new ArrayList<>();
List<String> list1 = new ArrayList<>();
list1.add("员工工资表");
list1.add(a);
list1.add("合同提成/绩效");
list1.add("员工名称");
head.add(list1);
List<String> list2 = new ArrayList<>();
list2.add("员工工资表");
list2.add(a);
list2.add("合同提成/绩效");
list2.add("员工日期");
head.add(list2);
List<String> list3 = new ArrayList<>();
list3.add("员工工资表");
list3.add(a);
list3.add("合同提成/绩效");
list3.add("员工工资");
head.add(list3);
mp.put("r1",head);
/** * 人2 */
List<List<String>> head1 = new ArrayList<>();
List<String> list4 = new ArrayList<>();
list4.add("员工工资表");
list4.add(b);
list4.add("合同提成/绩效");
list4.add("员工名称");
head1.add(list4);
List<String> list5 = new ArrayList<>();
list5.add("员工工资表");
list5.add(b);
list5.add("合同提成/绩效");
list5.add("员工日期");
head1.add(list5);
List<String> list6 = new ArrayList<>();
list6.add("员工工资表");
list6.add(b);
list6.add("合同提成/绩效");
list6.add("员工工资");
head1.add(list6);
mp.put("r2",head1);
return mp;
}
三、【多个sheet】参照EasyExcel官网导出数据在不同sheet上
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// 方法2: 如果写到不同的sheet 同一个对象
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 指定文件
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
// 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
for (int i = 0; i < 5; i++) {
// 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build();
//写入到excel和上面空开几行
writeSheet.setRelativeHeadRowIndex(1);
// 分页去数据库查询数据 这里可以去数据库查询每一页的数据
List<EmployeeSalaryPojo> data = data();
excelWriter.write(data, writeSheet);
}
} finally {
if(excelWriter!=null){
excelWriter.finish();
}
}
}
这里没有标题是因为 EmployeeSalaryPojo实体类中的前字段三个被注释了,解开前三个注释后三个就可以了
四、【同一sheet多个表】
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// 方法2: 如果写到不同的sheet 同一个对象
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 指定文件
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
// 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
//距离上边空一行
writeSheet.setRelativeHeadRowIndex(10);
// 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
// 第一次写入会创建头
excelWriter.write(data(), writeSheet, writeTable0);
// 第二次写如也会创建头,然后在第一次的后面写入数据
excelWriter.write(data(), writeSheet, writeTable1);
} finally {
if(excelWriter!=null){
excelWriter.finish();
}
}
}

五、【Excel中多个sheet,并且一个sheet多个表】
public static void simpleWrite() {
ExcelWriter excelWriter = null;
// 方法2: 如果写到不同的sheet 同一个对象
String fileName = "F:\\temporary\\" + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 指定文件
try {
excelWriter = EasyExcel.write(fileName, EmployeeSalaryPojo.class).build();
//设置动态标题
Map<String, List<List<String>>> head = head();
int i = 0;
for (String key : head.keySet()) {
i++;
/** * 1、获取到value; 获取到的List<List<String>>就表示是同一个sheet的数据 */
List<List<String>> lists = head.get(key);
// 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样【这里必须要加.needHead(Boolean.FALSE),否则会多处标题而没数据】
WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).needHead(Boolean.FALSE).build();
//动态标题
writeSheet.setHead(lists);
//写入到excel和上面空开几行
writeSheet.setRelativeHeadRowIndex(1);
/** * 2、相同数据写在同一个sheet里面 * 这里的j表示1个sheet会有几个表单数据 */
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();
}
}
}

边栏推荐
- matlab遍历图像、字符串数组等基本操作
- 微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
- 收到一封CTO来信,邀约面试机器学习工程师
- Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
- K-means based user portrait clustering model
- 基于K-means的用户画像聚类模型
- Spark interview questions
- CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
- Make a three digit number of all daffodils "recommended collection"
- A debugging to understand the slot mechanism of redis cluster
猜你喜欢

小 P 周刊 Vol.11

Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc

"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition

What is the difference between PMP and NPDP?

Spark面试题

名单揭晓 | 2021年度中国杰出知识产权服务团队

JS how to get a list of elements in a collection object

Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan

多种智能指针

K-means based user portrait clustering model
随机推荐
PHP reflective XSS, reflective XSS test and repair
ngnix基础知识
Why does blocprovider feel similar to provider?
matlab遍历图像、字符串数组等基本操作
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
面试题:MySQL的union all和union有什么区别、MySQL有哪几种join方式(阿里面试题)[通俗易懂]
pytest合集(2)— pytest運行方式
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
月入1W+的自媒体达人都会用到的运营工具
Matlab traverses images, string arrays and other basic operations
按照功能对Boost库进行分类
Fundamentals - IO intensive computing and CPU intensive computing
Communication between browser tab pages
比较版本号[双指针截取自己想要的字串]
String type conversion BigDecimal, date type
js数组拼接的四种方法[通俗易懂]
Difference and use between require and import
Significance and measures of security encryption of industrial control equipment