当前位置:网站首页>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 .
边栏推荐
- Advanced application of C # language
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
- Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
- 如何实现游戏中的在线计时器和离线计时器
- 通过POI追加数据到excel中小案例
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- 数学分析_笔记_第9章:曲线积分与曲面积分
- How to realize the Online timer and offline timer in the game
猜你喜欢

After the company went bankrupt, the blackstones came

关于 Notion-Like 工具的反思和畅想

Force buckle 729 My schedule I

数据库 逻辑处理功能

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

Fuzor 2020 software installation package download and installation tutorial

如何实现游戏中的在线计时器和离线计时器

Hiengine: comparable to the local cloud native memory database engine

ELK分布式日志分析系统部署(华为云)

Ten years at sea: old and new relay, dark horse rising
随机推荐
测试外包公司怎么样?
Do you know several assertion methods commonly used by JMeter?
Xaas trap: all things serve (possible) is not what it really needs
Fuzor 2020软件安装包下载及安装教程
Password reset of MariaDB root user and ordinary user
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
After the company went bankrupt, the blackstones came
HiEngine:可媲美本地的云原生内存数据库引擎
That's awesome. It's enough to read this article
Force buckle 1200 Minimum absolute difference
shell编程基础(第9篇:循环)
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
The problem of returning the longtext field in MySQL and its solution
How about testing outsourcing companies?
Teach you to deal with JS reverse picture camouflage hand in hand
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
HiEngine:可媲美本地的云原生内存数据库引擎
强化学习-学习笔记4 | Actor-Critic
Fuzor 2020軟件安裝包下載及安裝教程
成功入职百度月薪35K,2022Android开发面试解答