当前位置:网站首页>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 .
边栏推荐
- Fuzor 2020软件安装包下载及安装教程
- Is it safe for China Galaxy Securities to open an account? Securities account opening
- Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
- 国海证券在网上开户安全吗?
- Which securities company is better and which platform is safer for mobile account opening
- 手把手教你处理 JS 逆向之图片伪装
- S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
- Information / data
- Get wechat avatar and nickname with uniapp
- 40000 word Wenshuo operator new & operator delete
猜你喜欢

Force buckle 1200 Minimum absolute difference

C#应用程序界面开发基础——窗体控制(5)——分组类控件

Reflection and imagination on the notation like tool

Go语言 | 02 for循环及常用函数的使用
Mysql如何对json数据进行查询及修改

IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times

力扣 729. 我的日程安排表 I

Reinforcement learning - learning notes 4 | actor critical

MMO项目学习一:预热

Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
随机推荐
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
众昂矿业:2022年全球萤石行业市场供给现状分析
开源 SPL 消灭数以万计的数据库中间表
Vagrant2.2.6 supports virtualbox6.1
JAD installation, configuration and integration idea
After the company went bankrupt, the blackstones came
C# 语言的基本语法结构
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
C# 语言的高级应用
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
Information / data
Fuzor 2020 software installation package download and installation tutorial
What is the core value of testing?
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
100million single men and women supported an IPO with a valuation of 13billion
Debezium系列之:修改源码支持drop foreign key if exists fk
建议收藏,我的腾讯Android面试经历分享
MMO项目学习一:预热