当前位置:网站首页>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 .
边栏推荐
- Applet jumps to official account
- Using QA band and bit mask in Google Earth engine
- Implementation of go language for deleting duplicate items in sorting array
- 函数栈帧的创建和销毁
- Map in JS (including leetcode examples)
- Fabric. JS 3 APIs to set canvas width and height
- Thread pool batch processing data
- KMP idea and template code
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- 视差特效的原理和实现方法
猜你喜欢
Dark horse notes -- map set system
Fabric. JS iText superscript and subscript
JVM class loading mechanism
Visual studio import
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
Disable access to external entities in XML parsing
Principle and implementation of parallax effect
Fabric. JS iText sets the color and background color of the specified text
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
Online music player app
随机推荐
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Fabric. JS right click menu
There are duplicate elements in leetcode. Go implementation
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
Draw a wave chart_ Digital IC
LeetCode 1175. 质数排列(质数判断+组合数学)
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
Fabric. JS basic brush
Set the default style of scroll bar Google browser
Dark horse notes -- map set system
Storage of data
Leetcode18题 【四数之和】递归解法
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Fabric.js IText设置指定文字的颜色和背景色
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric.js IText 手动设置斜体
7.1 simulation summary
Gee: create a new feature and set corresponding attributes
Fabric. JS background is not affected by viewport transformation