当前位置:网站首页>Here comes a new chapter in the series of data conversion when exporting with easyexcel!
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
2022-07-02 05:27:00 【It snows at night when the boat is moored, and he can expect it】
Come on. ~ Use EasyExcel A new chapter in a series of data conversion when exporting !
A synopsis of antecedents
Before using EasyExcel When performing data export function , Type conversion processing ( Payment type 、 men and women 、 Other types, etc ) It's all for if else Judgment processing , Recently used Map To process , Record it here .
Code implementation
// Mode one
public class DemoTypeConverter implements Converter<String> {
public static final Map<String,String> DEMO_TYPE_MAP = ImmutableMap.<String,String>builder()
.put(" type 1","01")
.put(" type 2","02")
.put(" type 3","03")
.put(" type 4","04")
.put(" type 5","05")
.put(" type 6","06")
.put(" type 7","07").build();
@Override
public Class supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
if (StringUtils.isBlank(cellData.getStringValue())){
return "";
}
return DEMO_TYPE_MAP.getOrDefault(cellData.getStringValue(),"");
}
@Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return null;
}
}
// Mode two
public class DemoTypeConverter implements Converter<String> {
public static final Map<String,String> DEMO_TYPE_MAP = ImmutableMap.<String,String> of(
" type 1","01",
" type 2","02",
" type 3","03",
" type 4","04"
);
@Override
public Class supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
if (StringUtils.isBlank(cellData.getStringValue())){
return "";
}
return DEMO_TYPE_MAP.getOrDefault(cellData.getStringValue(),"");
}
@Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return null;
}
}
summary
As can be seen from the above code , The difference between the two methods is only Map Are constructed in different ways , Other code logic is the same . If there are many types, it is recommended to adopt method 1 , Less than or equal to 4 Method 2 .
边栏推荐
- Fabric. JS compact JSON
- Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
- Fabric.js 自由绘制矩形
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- [technical notes-08]
- Record sentry's path of stepping on the pit
- Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
- 黑马笔记---Map集合体系
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- Simply encapsulate JS and apply it
猜你喜欢

Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import

Pyechart1.19 national air quality exhibition

Ls1046nfs mount file system

Fabric. JS iText superscript and subscript

LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)

青训营--数据库实操项目

数据的储存

Fabric.js 右键菜单

Gee series: Unit 1 Introduction to Google Earth engine
![Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]](/img/1e/cf0aa09c2fce2278386f12eae4a6cd.jpg)
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
随机推荐
生成二维码
Gee series: Unit 1 Introduction to Google Earth engine
Fabric.js 激活输入框
Fabric.js 精简JSON
php/js cookie共享跨域的问题
Dark horse notes -- map set system
Fabric. JS gradient
Gee: create a new feature and set corresponding attributes
el form 表单validate成功后没有执行逻辑
Determine whether there is an element in the string type
线程池批量处理数据
Gee series: Unit 4 data import and export in Google Earth engine
指针使用详解
Feign realizes file uploading and downloading
Gee series: Unit 2 explore datasets
Simply encapsulate JS and apply it
Brew install * failed, solution
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
记录sentry的踩坑之路
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced