• 实现 easyExcel 导入导出自定义字典转换器


    一、easyExcel 字典转换器 Converter

    easyExcel 导入导出自定义字典转换器,包括导入字典转换以及导出字典转换。适配多个逗号分隔的字典值转换。

    1、excel 导入字典转换注解

    import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * excel导入字典转换注解
     * 

    * 将excel导入的字典label自动转换为字典code */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @JacksonAnnotationsInside @JsonSerialize(using = DictItemSerialize.class) public @interface ExcelDictItemLabel { /** * 字典type */ String type(); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2、excel 导出字典转换注解

    import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * excel导出字典转换注解
     * 

    * 将excel导出的字典code自动转换为字典label */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @JacksonAnnotationsInside @JsonSerialize(using = DictItemSerialize.class) public @interface ExcelDictItem { /** * 字典type */ String type(); }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3、自定义 excel 转换器

    import cn.hutool.core.util.StrUtil;
    import com.alibaba.excel.converters.Converter;
    import com.alibaba.excel.enums.CellDataTypeEnum;
    import com.alibaba.excel.metadata.CellData;
    import com.alibaba.excel.metadata.GlobalConfiguration;
    import com.alibaba.excel.metadata.property.ExcelContentProperty;
    import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
    import com.whitemeen.magic.common.core.constant.CacheConstants;
    import com.whitemeen.magic.common.core.constant.SecurityConstants;
    import com.whitemeen.magic.common.core.util.R;
    import com.whitemeen.magic.common.core.util.SpringContextHolder;
    import com.whitemeen.magic.upms.modules.admin.api.entity.SysDictItem;
    import com.whitemeen.magic.upms.modules.admin.api.feign.RemoteDictService;
    import com.whitemeen.magic.upms.modules.admin.config.ExcelDictItem;
    import com.whitemeen.magic.upms.modules.admin.config.ExcelDictItemLabel;
    import org.springframework.cache.Cache;
    import org.springframework.cache.CacheManager;
    
    import java.lang.reflect.Field;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.function.Function;
    import java.util.stream.Collectors;
    
    /**
     * Excel Converter字典转换器
     */
    public class ExcelDictConverter implements Converter<String> {
    
    	public ExcelDictConverter() {
    	}
    
    	@Override
    	public Class supportJavaTypeKey() {
    		return null;
    	}
    
    	@Override
    	public CellDataTypeEnum supportExcelTypeKey() {
    		return null;
    	}
    
    	/**
    	 * 将excel单元格数据转换成对象属性值(适配多个逗号分隔的字典值)
    	 *
    	 * @param cellData             单元格的数据
    	 * @param excelContentProperty excel每一行的数据内容
    	 * @param globalConfiguration  global全局配置
    	 * @return 转换后的对象属性值
    	 * @throws Exception 异常
    	 */
    	@Override
    	public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    		// 获取字典类型
    		Field field = excelContentProperty.getField();
    		ExcelDictItemLabel excel = field.getAnnotation(ExcelDictItemLabel.class);
    		String dictType = excel.type();
    
    		// 为空返回
    		String dictLabel = cellData.getStringValue();
    		if (StrUtil.isBlank(dictLabel)) {
    			return dictLabel;
    		}
    
    		// 查询字典
    		CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);
    		Cache dictCache = cacheManager.getCache(CacheConstants.DICT_DETAILS);
    		R<List<SysDictItem>> dictItemListR = dictCache.get(dictType, R.class);
    
    		if (dictItemListR == null) {
    			RemoteDictService dictService = SpringContextHolder.getBean(RemoteDictService.class);
    			dictItemListR = dictService.customize(dictType, SecurityConstants.FROM_IN);
    		}
    		if (dictItemListR == null || CollectionUtils.isEmpty(dictItemListR.getData())) {
    			return dictLabel;
    		}
    
    		// 将字典项根据label分组
    		Map<String, SysDictItem> dictItemLabelMap = dictItemListR.getData()
    				.stream().collect(Collectors.toMap(SysDictItem::getLabel, Function.identity(), (v1, v2) -> v2));
    
    		// 存在多个字典项值
    		List<String> dictLabelList = Arrays.stream(dictLabel.split(",")).collect(Collectors.toList());
    		StringBuilder dictValues = new StringBuilder();
    
    		// 字典转换拼接
    		for (String sourcesDictLabel : dictLabelList) {
    			SysDictItem sysDictItem = dictItemLabelMap.get(sourcesDictLabel);
    			if (sysDictItem == null) {
    				continue;
    			}
    
    			if (StrUtil.isBlank(dictValues)) {
    				dictValues.append(sysDictItem.getItemValue());
    			} else {
    				dictValues.append(",").append(sysDictItem.getItemValue());
    			}
    		}
    
    		return String.valueOf(dictValues);
    	}
    
    	
    //	/**
    //	 * 将excel单元格数据转换成对象属性值(仅适配单个字典值)
    //	 *
    //	 * @param cellData             单元格的数据
    //	 * @param excelContentProperty excel每一行的数据内容
    //	 * @param globalConfiguration  global全局配置
    //	 * @return 转换后的对象属性值
    //	 * @throws Exception 异常
    //	 */
    //	@Override
    //	public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    //		// 获取字典类型
    //		Field field = excelContentProperty.getField();
    //		ExcelDictItemLabel excel = field.getAnnotation(ExcelDictItemLabel.class);
    //		String dictType = excel.type();
    //
    //		String dictLabel = cellData.getStringValue();
    //		if (StrUtil.isBlank(dictLabel)) {
    //			return dictLabel;
    //		}
    //		// 查询字典
    //		RemoteDictService remoteDictService = SpringContextHolder.getBean(RemoteDictService.class);
    //		R> dictItemsR = remoteDictService.getDictByType(dictType);
    //		if (CollectionUtil.isEmpty(dictItemsR.getData())) {
    //			return dictLabel;
    //		}
    //		// 进行字典翻译
    //		String javaData = dictLabel;
    //		List dictItems = dictItemsR.getData();
    //		for (SysDictItem dictItem : dictItems) {
    //			if (StrUtil.equals(dictLabel, dictItem.getLabel())) {
    //				javaData = dictItem.getItemValue();
    //				break;
    //			}
    //		}
    //		return javaData;
    //	}
    
    	/**
    	 * 将对象属性值转换成excel单元格数据(适配多个逗号分隔的字典值)
    	 *
    	 * @param dictValue            对象的属性值
    	 * @param excelContentProperty excel每一行的数据内容
    	 * @param globalConfiguration  global全局配置
    	 * @return 转换后的对象属性值
    	 * @throws Exception 异常
    	 */
    	@Override
    	public CellData convertToExcelData(String dictValue, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    		// 获取字典类型
    		Field field = excelContentProperty.getField();
    		ExcelDictItem excel = field.getAnnotation(ExcelDictItem.class);
    		String dictType = excel.type();
    
    		// 为空返回
    		if (StrUtil.isBlank(dictValue)) {
    			return new CellData(dictValue);
    		}
    
    		// 查询字典
    		CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);
    		Cache dictCache = cacheManager.getCache(CacheConstants.DICT_DETAILS);
    		R<List<SysDictItem>> dictItemListR = dictCache.get(dictType, R.class);
    
    		if (dictItemListR == null) {
    			RemoteDictService dictService = SpringContextHolder.getBean(RemoteDictService.class);
    			dictItemListR = dictService.customize(dictType, SecurityConstants.FROM_IN);
    		}
    		if (dictItemListR == null || CollectionUtils.isEmpty(dictItemListR.getData())) {
    			return new CellData(dictValue);
    		}
    
    		// 将字典项根据value分组
    		Map<String, SysDictItem> dictItemValueMap = dictItemListR.getData()
    				.stream().collect(Collectors.toMap(SysDictItem::getItemValue, Function.identity(), (v1, v2) -> v2));
    
    		// 存在多个字典项值
    		List<String> dictValueList = Arrays.stream(dictValue.split(",")).collect(Collectors.toList());
    		StringBuilder dictLabels = new StringBuilder();
    
    		// 字典转换拼接
    		for (String sourcesDictLabel : dictValueList) {
    			SysDictItem sysDictItem = dictItemValueMap.get(sourcesDictLabel);
    			if (sysDictItem == null) {
    				continue;
    			}
    
    			if (StrUtil.isBlank(dictLabels)) {
    				dictLabels.append(sysDictItem.getLabel());
    			} else {
    				dictLabels.append(",").append(sysDictItem.getLabel());
    			}
    		}
    
    		if(StrUtil.isBlank(String.valueOf(dictLabels))){
    			return new CellData(dictValue);
    		}
    		return new CellData(String.valueOf(dictLabels));
    	}
    	
    //	/**
    //	 * 将对象属性值转换成excel单元格数据(仅适配单个字典值)
    //	 *
    //	 * @param dictValue            对象的属性值
    //	 * @param excelContentProperty excel每一行的数据内容
    //	 * @param globalConfiguration  global全局配置
    //	 * @return 转换后的对象属性值
    //	 * @throws Exception 异常
    //	 */
    //	@Override
    //	public CellData convertToExcelData(String dictValue, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    //		// 获取字典类型
    //		Field field = excelContentProperty.getField();
    //		String fieldName = field.getName();
    //		DictItem excel = field.getAnnotation(DictItem.class);
    //		String dictType = excel.type();
    //		// 查询字典
    //		RemoteDictService remoteDictService = SpringContextHolder.getBean(RemoteDictService.class);
    //		R> dictItemsR = remoteDictService.getDictByType(dictType);
    //		if (CollectionUtil.isEmpty(dictItemsR.getData())) {
    //			return new CellData(dictValue);
    //		}
    //		// 进行字典翻译
    //		String excelData = dictValue;
    //		List dictItems = dictItemsR.getData();
    //		for (SysDictItem dictItem : dictItems) {
    //			if (StrUtil.equals(dictValue, dictItem.getItemValue())) {
    //				excelData = dictItem.getLabel();
    //				break;
    //			}
    //		}
    //		return new CellData(excelData);
    //	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239

    4、使用 excel 字典转换注解

    
    	/**
    	 * 性别label(用于接收与展现真实的excel内容)
    	 */
    	@ExcelProperty(value = "性别", converter = ExcelDictConverter.class)
    	@ExcelDictItemLabel(type = "gender")
    	private String genderLabel;
    
    	/**
    	 * 性别code(用来承载java对象属性值)
    	 */
    	@ExcelIgnore
    	@ExcelDictItem(type = "gender")
    	private String gender;
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    数据处理生产环境_获取当前日期的前一天日期
    kafka知识笔记
    Py之trl:trl(一款采用强化学习训练Transformer语言模型和稳定扩散模型的全栈库)的简介、安装、使用方法之详细攻略
    Java题目集
    Day15|104.二叉树的最大深度
    R语言计算代码的运行时间:使用tictoc包计算代码的运行时间长短、将toc的结果赋值值变量、可以获取详细信息(开始时间、结束时间、tic内容等)
    Python的os和Pillow库来实现遍历所有子文件夹并将BMP图片转换为PNG格式
    Maven3种打包方式之一maven-assembly-plugin的使用
    无序链表(顺序查找)和有序数组(二分查找)-基础实现-符号表(二)-数据结构和算法(Java)
    如何自学网络安全?零基础入门看这篇就够了(含路线图)_网络安全自学
  • 原文地址:https://blog.csdn.net/demo_yo/article/details/132778472