当前位置:网站首页>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 .
边栏推荐
- Collectors.groupingBy 排序
- el form 表单validate成功后没有执行逻辑
- There are duplicate elements in leetcode. Go implementation
- Visual Studio导入
- Gee series: unit 8 time series analysis in Google Earth engine [time series]
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
- [technical notes-08]
- Using QA band and bit mask in Google Earth engine
- 在线音乐播放器app
- Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
猜你喜欢
随机推荐
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Fabric. JS iText sets the color and background color of the specified text
Php/js cookie sharing across domains
Fabric. JS iText superscript and subscript
函数栈帧的创建和销毁
KMP idea and template code
Fabric.js IText设置指定文字的颜色和背景色
LeetCode 1175. 质数排列(质数判断+组合数学)
生成二维码
Collectors. Groupingby sort
Nodejs (02) - built in module
Record sentry's path of stepping on the pit
Online music player app
小程序跳装到公众号
Storage of data
Gee series: Unit 2 explore datasets
Gee: remote sensing image composite and mosaic
MySQL foundation --- query (learn MySQL foundation in 1 day)
Gee series: unit 8 time series analysis in Google Earth engine [time series]
No logic is executed after the El form is validated successfully