淺析EasyExcel如何導(dǎo)出自動回顯中文
引言
在實(shí)際業(yè)務(wù)中,我們經(jīng)常需要將數(shù)據(jù)庫中的碼值(如 1, 2, 3)在導(dǎo)出Excel時顯示為中文(如“進(jìn)行中”、“已完成”、“已取消”),而在導(dǎo)入Excel時,用戶填寫的中文需要自動轉(zhuǎn)換為對應(yīng)的碼值。本文將介紹如何通過全局轉(zhuǎn)換器(GlobalCodeConverter) 和自定義注解實(shí)現(xiàn)這一功能,結(jié)合數(shù)據(jù)庫動態(tài)查詢碼表,提升代碼復(fù)用性和可維護(hù)性。
一、需求場景
假設(shè)有一個訂單表,字段 trans_status 存儲狀態(tài)碼(1: 進(jìn)行中,2: 已完成,3: 已取消),需滿足以下需求
- 導(dǎo)出Excel時:將 trans_status=1 轉(zhuǎn)換為“進(jìn)行中”顯示
- 導(dǎo)入Excel時:用戶輸入“進(jìn)行中”,自動轉(zhuǎn)換為 1 存儲到數(shù)據(jù)庫
- 支持多碼表:如性別、優(yōu)先級等字段也需要類似處理
二、實(shí)現(xiàn)思路
自定義注解:標(biāo)記需要轉(zhuǎn)換的字段,并指定碼表類型
全局轉(zhuǎn)換器:攔截注解標(biāo)記的字段,動態(tài)查詢數(shù)據(jù)庫獲取碼值映射關(guān)系
緩存優(yōu)化:減少頻繁查詢數(shù)據(jù)庫,提升性能
三、代碼實(shí)現(xiàn)
1、自定義注解 @ExcelSelected
用于標(biāo)記需要轉(zhuǎn)換的字段,指定碼表類型
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelSelected {
/**
* 碼表類型(如 trans_status)
*/
String codeField();
}
2、碼表實(shí)體與數(shù)據(jù)庫查詢
設(shè)計(jì)碼表結(jié)構(gòu)(如 sys_dict)
CREATE TABLE sys_dict (
type VARCHAR(50) COMMENT '碼表類型(如trans_status)',
code VARCHAR(20) COMMENT '碼值',
name VARCHAR(50) COMMENT '顯示名稱'
);
3、全局轉(zhuǎn)換器 GlobalCodeConverter
實(shí)現(xiàn) Converter 接口,處理導(dǎo)入導(dǎo)出時的轉(zhuǎn)換邏輯
public class GlobalCodeConverter implements Converter<String> {
private static final Map<String, Map<String, String>> CACHE = new ConcurrentHashMap<>();
@Override
public Class<String> supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
// 讀取Excel時的轉(zhuǎn)換(中文->碼值)
@Override
public String convertToJavaData(ReadConverterContext<?> context) {
String cellValue = context.getReadCellData().getStringValue();
Field field = context.getContentProperty().getField();
ExcelSelected annotation = field.getAnnotation(ExcelSelected.class);
if (annotation != null) {
Map<String, String> codeMap = getCodeMap(annotation.codeField());
return codeMap.entrySet().stream()
.filter(entry -> entry.getValue().equals(cellValue))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
}
return cellValue;
}
// 寫入Excel時的轉(zhuǎn)換(碼值->中文)
@Override
public WriteCellData<String> convertToExcelData(WriteConverterContext<String> context) {
String value = context.getValue();
Field field = context.getContentProperty().getField();
ExcelSelected annotation = field.getAnnotation(ExcelSelected.class);
if (annotation != null) {
Map<String, String> codeMap = getCodeMap(annotation.codeField());
return new WriteCellData<>(codeMap.get(value));
}
return new WriteCellData<>(value);
}
// 獲取碼表數(shù)據(jù)
private Map<String, String> getCodeMap(String codeType) {
// 帶緩存的碼表查詢
return CACHE.computeIfAbsent(codeType, k -> {
// 實(shí)際查詢數(shù)據(jù)庫的代碼(示例)
Map<String, String> map = new HashMap<>();
map.put("1", "進(jìn)行中");
map.put("2", "已完成");
map.put("3", "已取消");
return map;
});
}
}
4、實(shí)體類使用注解
在需要轉(zhuǎn)換的字段上添加 @ExcelSelected
public class OrderVO {
@ExcelProperty("訂單狀態(tài)")
@ExcelSelected(codeField = "trans_status")
private String transStatus;
}
5、注冊全局轉(zhuǎn)換器
在導(dǎo)出/導(dǎo)入時注冊轉(zhuǎn)換器
// 導(dǎo)出
EasyExcel.write(fileName, OrderVO.class)
.registerConverter(new GlobalCodeConverter())
.sheet().doWrite(orders);
// 導(dǎo)入
EasyExcel.read(fileName, OrderVO.class, new PageReadListener<>(list -> {}))
.registerConverter(new GlobalCodeConverter())
.sheet().doRead();
四、效果驗(yàn)證
導(dǎo)出Excel:trans_status=1 顯示為“進(jìn)行中”
導(dǎo)入Excel:用戶輸入“進(jìn)行中”,自動轉(zhuǎn)換為 1 存入數(shù)據(jù)庫
五、總結(jié)
通過自定義注解和全局轉(zhuǎn)換器,實(shí)現(xiàn)了碼值與中文的動態(tài)轉(zhuǎn)換,代碼簡潔且易于擴(kuò)展。這里可以結(jié)合之前寫的文章EasyExcel自定義下拉注解的三種實(shí)現(xiàn)方式總結(jié),進(jìn)一步提升用戶體驗(yàn)。
到此這篇關(guān)于淺析EasyExcel如何導(dǎo)出自動回顯中文的文章就介紹到這了,更多相關(guān)EasyExcel導(dǎo)出自動回顯中文內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot?整合?EasyExcel?實(shí)現(xiàn)自由導(dǎo)入導(dǎo)出功能
- SpringBoot整合EasyExcel實(shí)現(xiàn)批量導(dǎo)入導(dǎo)出
- 使用Easyexcel實(shí)現(xiàn)不同場景的數(shù)據(jù)導(dǎo)出功能
- 使用EasyExcel實(shí)現(xiàn)百萬級別數(shù)據(jù)導(dǎo)出的代碼示例
- 使用SpringBoot與EasyExcel實(shí)現(xiàn)復(fù)雜的導(dǎo)入導(dǎo)出
- Java使用EasyExcel實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
- java利用easyexcel實(shí)現(xiàn)導(dǎo)入與導(dǎo)出功能
相關(guān)文章
Java實(shí)現(xiàn)把文件及文件夾壓縮成zip
這篇文章主要介紹了Java實(shí)現(xiàn)把文件及文件夾壓縮成zip,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
spring boot項(xiàng)目如何采用war在tomcat容器中運(yùn)行
這篇文章主要介紹了spring boot項(xiàng)目如何采用war在tomcat容器中運(yùn)行呢,主要講述將SpringBoot打成war包并放入tomcat中運(yùn)行的方法分享,需要的朋友可以參考下2022-11-11
SpringBoot整合RabbitMQ實(shí)現(xiàn)六種工作模式的示例
這篇文章主要介紹了SpringBoot整合RabbitMQ實(shí)現(xiàn)六種工作模式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

