当前位置:网站首页>Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
2022-07-05 19:33:00 【lang20150928】
In the project , Many colleagues like to use Map To store data , In particular, query data from the database and store it in List<Map<String,Object>> among . This will lead to the passage of excel The data is disordered when exporting the template .
At present easyexcel edition
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
Suppose the following template exists (limitManage2.xlsx).
date | Operator | Start date of quota period | Due date of quota period | Total amount ( element ) | Remaining amount ( element ) | remarks | state | Name of organization | Collected limit ( element ) | remarks 2 |
---|---|---|---|---|---|---|---|---|---|---|
{bean.CREATE_DATE} | {bean.CREATOR} | {bean.LIMIT_CYCLE_START} | {bean.LIMIT_CYCLE_MTR} | {bean.TOTAL_LIMIT} | {bean.SURPLUS_LIMIT} | {bean.REMARK} | {bean.STATUS} | {bean.BRANCH_INSTITUTION_NAME} | {bean.GET_LIMIT} | {bean.fillData3.REMARK} |
First, through List<Map<String,Object>> Fill in the data ( Willingly REMARK The value part of the field is set to null , The rule is that the index with even number in the list is empty )
private static Map<String, Object> data2(){
Map<String, Object> params = new HashMap<>();
List<Map<String, String>> result = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Map<String, String> curMap = new HashMap<>();
curMap.put("CREATE_DATE", "2018-06-12");
curMap.put("STATUS", " In force ");
curMap.put("LIMIT_CYCLE_START", "2018-06-12");
curMap.put("LIMIT_CYCLE_MTR", "2018-06-12");
curMap.put("BRANCH_INSTITUTION_NAME", " Asset management department ");
curMap.put("SURPLUS_LIMIT", "88888888" + i);
curMap.put("CREATOR", " System administrator " + i);
curMap.put("GET_LIMIT", "101000000");
curMap.put("PK", "2417");
curMap.put("TOTAL_LIMIT", "88888888");
if (i % 2 == 0) {
curMap.put("REMARK", " Customized quota " + i);
}else {
// curMap.put("REMARK","");
}
result.add(curMap);
}
params.put("bean", result);
return params;
}
Create test class
@Test
public void compositeFillMapIssue() {
String templateFileName =
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "limitManage2.xlsx";
System.out.println(new File(templateFileName).getPath());
String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx";
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
Map<String, Object> data2 = data2();
FillConfig fillConfig = FillConfig.builder().build();
data2.forEach((key,value)->{
excelWriter.fill(new FillWrapper(key, (List<Map<String, String>>)value),fillConfig, writeSheet);
});
}
System.out.println(new File(fileName).getPath());
}
Test classpath src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
Test template path src/test/resources/demo/fill/limitManage2.xlsx
The results are shown below
Due date of quota period | Total amount ( element ) | Remaining amount ( element ) | remarks | state | Name of organization | Collected limit ( element ) | remarks 2 |
---|---|---|---|---|---|---|---|
2018-06-12 | 88888888 | 888888880 | Customized quota 0 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888881 | Customized quota 2 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888882 | Customized quota 4 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888883 | Customized quota 6 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888884 | Customized quota 8 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888885 | Customized quota 10 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888886 | Customized quota 12 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888887 | Customized quota 14 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888888 | Customized quota 16 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888889 | Customized quota 18 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888810 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888811 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888812 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888813 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888814 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888815 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888816 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888817 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888818 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888819 | In force | Asset management department | 101000000 |
From the above results, it is not difficult to see , The information in the remarks column is confused .
Found by test , take Map Turn it into an object .
pojo The definition is as follows
package com.alibaba.easyexcel.test.demo.fill;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
public class FillData2 {
private String CREATE_DATE;
private String STATUS;
private String LIMIT_CYCLE_START;
private String LIMIT_CYCLE_MTR;
private String BRANCH_INSTITUTION_NAME;
private String SURPLUS_LIMIT;
private String CREATOR;
private String GET_LIMIT;
private String PK;
private String TOTAL_LIMIT;
private String REMARK;
private FillData3 fillData3;
}
package com.alibaba.easyexcel.test.demo.fill;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@EqualsAndHashCode
public class FillData3 {
private String REMARK3;
}
Create test data
private static Map<String, List<FillData2>> data3(){
Map<String, List<FillData2>> params = new HashMap<>();
List<FillData2> result = new ArrayList<>();
for (int i = 0; i < 20; i++) {
FillData2 curMap = new FillData2();
curMap.setCREATE_DATE("2018-06-12");
curMap.setSTATUS(" In force ");
curMap.setLIMIT_CYCLE_START("2018-06-12");
curMap.setLIMIT_CYCLE_MTR("2018-06-12");
curMap.setBRANCH_INSTITUTION_NAME(" Asset management department ");
curMap.setSURPLUS_LIMIT("88888888" + i);
curMap.setCREATOR(" System administrator " + i);
curMap.setGET_LIMIT("101000000");
curMap.setPK("2417");
curMap.setTOTAL_LIMIT("88888888");
if (i % 2 == 0) {
curMap.setREMARK(" Customized quota " + i);
}else {
// curMap.put("REMARK","");
}
FillData3 fillData3 = new FillData3();
fillData3.setREMARK3(curMap.getREMARK() +">>");
curMap.setFillData3(fillData3);
result.add(curMap);
}
params.put("bean", result);
return params;
}
Create test class
@Test
public void compositeFillMapIssue3() {
String templateFileName =
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "limitManage2.xlsx";
System.out.println(new File(templateFileName).getPath());
String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx";
// programme 1
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
Map<String, List<FillData2>> data2 = data3();
data2.forEach((key,value)->{
excelWriter.fill(new FillWrapper(key, value), writeSheet);
});
}
System.out.println(new File(fileName).getPath());
}
The test results are shown below
Due date of quota period | Total amount ( element ) | Remaining amount ( element ) | remarks | state | Name of organization | Collected limit ( element ) | remarks 2 |
---|---|---|---|---|---|---|---|
2018-06-12 | 88888888 | 888888880 | Customized quota 0 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888881 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 888888882 | Customized quota 2 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888883 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 888888884 | Customized quota 4 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888885 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 888888886 | Customized quota 6 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888887 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 888888888 | Customized quota 8 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 888888889 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888810 | Customized quota 10 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888811 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888812 | Customized quota 12 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888813 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888814 | Customized quota 14 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888815 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888816 | Customized quota 16 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888817 | In force | Asset management department | 101000000 | ||
2018-06-12 | 88888888 | 8888888818 | Customized quota 18 | In force | Asset management department | 101000000 | |
2018-06-12 | 88888888 | 8888888819 | In force | Asset management department | 101000000 |
As can be seen from the test results , The problem of disordered null data above has been solved , But there is no data in the last column . The corresponding template is {bean.fillData3.REMARK}
, As you can see from here easyexcel Template export with complex object data in an object is not supported .
边栏推荐
- IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
- Go语言学习教程(十六)
- 如何在2022年更明智地应用智能合约?
- 不愧是大佬,字节大牛耗时八个月又一力作
- MMO项目学习一:预热
- JAD的安装、配置及集成IDEA
- 开源 SPL 消灭数以万计的数据库中间表
- 手机开户选择哪家券商公司比较好哪家平台更安全
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
- MMO project learning 1: preheating
猜你喜欢
随机推荐
Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
How to apply smart contracts more wisely in 2022?
ELK分布式日志分析系统部署(华为云)
PG基础篇--逻辑结构管理(用户及权限管理)
Mariadb root用户及普通用户的密码 重置
Debezium系列之:修改源码支持drop foreign key if exists fk
Force buckle 729 My schedule I
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
vagrant2.2.6支持virtualbox6.1版本
aggregate
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
从零实现深度学习框架——LSTM从理论到实战【实战】
Fundamentals of machine learning (III) -- KNN / naive Bayes / cross validation / grid search
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
测试外包公司怎么样?
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Do you know several assertion methods commonly used by JMeter?
PHP利用ueditor实现上传图片添加水印
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
如何实现游戏中的在线计时器和离线计时器