当前位置:网站首页>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 .
边栏推荐
- HiEngine:可媲美本地的云原生内存数据库引擎
- 使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
- Fuzor 2020 software installation package download and installation tutorial
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
- C application interface development foundation - form control (5) - grouping control
- Password reset of MariaDB root user and ordinary user
- 大厂面试必备技能,2022Android不死我不倒
- Fuzor 2020軟件安裝包下載及安裝教程
- Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
- Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
猜你喜欢
JMeter 常用的几种断言方法,你会了吗?
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
如何在2022年更明智地应用智能合约?
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
How MySQL queries and modifies JSON data
Android interview, Android audio and video development
成功入职百度月薪35K,2022Android开发面试解答
Hiengine: comparable to the local cloud native memory database engine
随机推荐
14、用户、组和权限(14)
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
JAD installation, configuration and integration idea
HiEngine:可媲美本地的云原生内存数据库引擎
Fundamentals of machine learning (III) -- KNN / naive Bayes / cross validation / grid search
100million single men and women supported an IPO with a valuation of 13billion
Do you know several assertion methods commonly used by JMeter?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
建议收藏,我的腾讯Android面试经历分享
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
开源 SPL 消灭数以万计的数据库中间表
成功入职百度月薪35K,2022Android开发面试解答
Fundamentals of shell programming (Part 8: branch statements -case in)
Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
如何在2022年更明智地应用智能合约?
集合
Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
HAC cluster modifying administrator user password
力扣 729. 我的日程安排表 I