当前位置:网站首页>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 .
边栏推荐
- Leetcode18题 【四数之和】递归解法
- Reflection of the soul of the frame (important knowledge)
- Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
- brew install * 失败,解决方法
- Mysql基础---查询(1天学会mysql基础)
- Collectors. Groupingby sort
- ubuntu20.04安装mysql8
- Operator details
- Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
- 7.1 Résumé du concours de simulation
猜你喜欢
随机推荐
Dark horse notes -- Set Series Collection
Online music player app
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
Gee series: Unit 1 Introduction to Google Earth engine
Fabric.js 圆形笔刷
Paddlepaddle project source code
Gee: create a new feature and set corresponding attributes
Fabric. JS iText sets the color and background color of the specified text
Fabric.js 基础笔刷
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
7.1模拟赛总结
Disable access to external entities in XML parsing
Gee series: Unit 2 explore datasets
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
Fabric. JS right click menu
Generate QR code
Fabric.js IText 上标和下标
操作符详解








