当前位置:网站首页>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 .
边栏推荐
- Ten years at sea: old and new relay, dark horse rising
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
- Shell编程基础(第8篇:分支语句-case in)
- PHP uses ueditor to upload pictures and add watermarks
- HAC cluster modifying administrator user password
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
- Mysql如何对json数据进行查询及修改
- Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
- Android interview, Android audio and video development
猜你喜欢
How MySQL queries and modifies JSON data

What do software test engineers do? How about the prospect of treatment?

Fuzor 2020軟件安裝包下載及安裝教程

Advanced application of C # language

How to convert word into PDF? Word to PDF simple way to share!

详解SQL中Groupings Sets 语句的功能和底层实现逻辑

太牛了,看这篇足矣了

UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术

Do you know several assertion methods commonly used by JMeter?

MMO項目學習一:預熱
随机推荐
中国银河证券开户安全吗 证券开户
That's awesome. It's enough to read this article
MySQL中字段类型为longtext的值导出后显示二进制串方式
PG基础篇--逻辑结构管理(用户及权限管理)
What are general items
Mysql如何对json数据进行查询及修改
How to apply smart contracts more wisely in 2022?
flume系列之:拦截器过滤数据
Reptile exercises (II)
MySql中的longtext字段的返回问题及解决
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
太牛了,看这篇足矣了
14、用户、组和权限(14)
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
Reflection and imagination on the notation like tool
100million single men and women supported an IPO with a valuation of 13billion
What are the reliable domestic low code development platforms?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
HiEngine:可媲美本地的云原生内存数据库引擎