当前位置:网站首页>来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
2022-07-02 05:24:00 【泊船夜雪,他年可期】
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
前情概要
之前使用 EasyExcel 进行数据导出功能的时候,类型转换处理(支付类型、男女、其他类型等)都是用的 if else 进行的判断处理,最近使用了Map 来进行处理,在这里记录一下。
代码实现
// 方式一
public class DemoTypeConverter implements Converter<String> {
public static final Map<String,String> DEMO_TYPE_MAP = ImmutableMap.<String,String>builder()
.put("类型1","01")
.put("类型2","02")
.put("类型3","03")
.put("类型4","04")
.put("类型5","05")
.put("类型6","06")
.put("类型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;
}
}
// 方式二
public class DemoTypeConverter implements Converter<String> {
public static final Map<String,String> DEMO_TYPE_MAP = ImmutableMap.<String,String> of(
"类型1","01",
"类型2","02",
"类型3","03",
"类型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;
}
}
总结
以上代码可以看出,两种方式的区别只是 Map 的构造方式不同,其他代码逻辑都一样。如果类型较多建议采用方式一,小于等于 4 种采用方式二。
边栏推荐
- Storage of data
- Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
- Fabric.js 激活输入框
- 黑马笔记---Map集合体系
- kmp思想及模板代码
- Johnson–Lindenstrauss Lemma(2)
- Feign realizes file uploading and downloading
- Fabric.js IText 手动设置斜体
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
Fabric. JS iText sets the color and background color of the specified text
brew install * 失败,解决方法
LS1046nfs挂载文件系统
Fabric. JS gradient
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Nodejs (02) - built in module
黑马笔记---Map集合体系
Collectors. Groupingby sort
摆正元素(带过渡动画)
随机推荐
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric.js 居中元素
Fabric. JS free draw rectangle
Fabric.js 3个api设置画布宽高
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
h5跳小程序
Fabric.js 将本地图像上传到画布背景
4. Flask cooperates with a tag to link internal routes
Disable access to external entities in XML parsing
Implementation of leetcode two number addition go
Fabric.js IText设置指定文字的颜色和背景色
Fabric.js 右键菜单
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
7.1模擬賽總結
Fabric.js 圆形笔刷
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
LS1046nfs挂载文件系统
Gee: explore the characteristics of precipitation change in the Yellow River Basin in the past 10 years [pixel by pixel analysis]
函数栈帧的创建和销毁
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)