作者 RuoYi

Excel导出导入支持dictType字典类型

@@ -30,6 +30,11 @@ public @interface Excel @@ -30,6 +30,11 @@ public @interface Excel
30 public String dateFormat() default ""; 30 public String dateFormat() default "";
31 31
32 /** 32 /**
  33 + * 如果是字典类型,请设置字典的type值
  34 + */
  35 + public String dictType() default "";
  36 +
  37 + /**
33 * 读取内容转表达式 (如: 0=男,1=女,2=未知) 38 * 读取内容转表达式 (如: 0=男,1=女,2=未知)
34 */ 39 */
35 public String readConverterExp() default ""; 40 public String readConverterExp() default "";
@@ -115,4 +120,4 @@ public @interface Excel @@ -115,4 +120,4 @@ public @interface Excel
115 return this.value; 120 return this.value;
116 } 121 }
117 } 122 }
118 -} 123 +}
@@ -2,7 +2,6 @@ package com.ruoyi.common.utils; @@ -2,7 +2,6 @@ package com.ruoyi.common.utils;
2 2
3 import java.util.Collection; 3 import java.util.Collection;
4 import java.util.List; 4 import java.util.List;
5 -  
6 import com.ruoyi.common.constant.Constants; 5 import com.ruoyi.common.constant.Constants;
7 import com.ruoyi.common.core.domain.entity.SysDictData; 6 import com.ruoyi.common.core.domain.entity.SysDictData;
8 import com.ruoyi.common.core.redis.RedisCache; 7 import com.ruoyi.common.core.redis.RedisCache;
@@ -44,6 +43,58 @@ public class DictUtils @@ -44,6 +43,58 @@ public class DictUtils
44 } 43 }
45 44
46 /** 45 /**
  46 + * 根据字典类型和字典值获取字典标签
  47 + *
  48 + * @param dictType 字典类型
  49 + * @param dictValue 字典值
  50 + * @return 字典标签
  51 + */
  52 + public static String getDictLabel(String dictType, String dictValue)
  53 + {
  54 + if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictValue))
  55 + {
  56 + List<SysDictData> datas = getDictCache(dictType);
  57 + if (StringUtils.isNotEmpty(datas))
  58 + {
  59 + for (SysDictData dict : datas)
  60 + {
  61 + if (dictValue.equals(dict.getDictValue()))
  62 + {
  63 + return dict.getDictLabel();
  64 + }
  65 + }
  66 + }
  67 + }
  68 + return dictValue;
  69 + }
  70 +
  71 + /**
  72 + * 根据字典类型和字典标签获取字典值
  73 + *
  74 + * @param dictType 字典类型
  75 + * @param dictLabel 字典标签
  76 + * @return 字典值
  77 + */
  78 + public static String getDictValue(String dictType, String dictLabel)
  79 + {
  80 + if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel))
  81 + {
  82 + List<SysDictData> datas = getDictCache(dictType);
  83 + if (StringUtils.isNotEmpty(datas))
  84 + {
  85 + for (SysDictData dict : datas)
  86 + {
  87 + if (dictLabel.equals(dict.getDictLabel()))
  88 + {
  89 + return dict.getDictValue();
  90 + }
  91 + }
  92 + }
  93 + }
  94 + return dictLabel;
  95 + }
  96 +
  97 + /**
47 * 清空字典缓存 98 * 清空字典缓存
48 */ 99 */
49 public static void clearDictCache() 100 public static void clearDictCache()
@@ -50,6 +50,7 @@ import com.ruoyi.common.core.domain.AjaxResult; @@ -50,6 +50,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
50 import com.ruoyi.common.core.text.Convert; 50 import com.ruoyi.common.core.text.Convert;
51 import com.ruoyi.common.exception.CustomException; 51 import com.ruoyi.common.exception.CustomException;
52 import com.ruoyi.common.utils.DateUtils; 52 import com.ruoyi.common.utils.DateUtils;
  53 +import com.ruoyi.common.utils.DictUtils;
53 import com.ruoyi.common.utils.StringUtils; 54 import com.ruoyi.common.utils.StringUtils;
54 import com.ruoyi.common.utils.reflect.ReflectUtils; 55 import com.ruoyi.common.utils.reflect.ReflectUtils;
55 56
@@ -270,7 +271,11 @@ public class ExcelUtil<T> @@ -270,7 +271,11 @@ public class ExcelUtil<T>
270 } 271 }
271 else if (StringUtils.isNotEmpty(attr.readConverterExp())) 272 else if (StringUtils.isNotEmpty(attr.readConverterExp()))
272 { 273 {
273 - val = reverseByExp(String.valueOf(val), attr.readConverterExp()); 274 + val = reverseByExp(Convert.toStr(val), attr.readConverterExp());
  275 + }
  276 + else if (StringUtils.isNotEmpty(attr.dictType()))
  277 + {
  278 + val = reverseDictByExp(attr.dictType(), Convert.toStr(val));
274 } 279 }
275 ReflectUtils.invokeSetter(entity, propertyName, val); 280 ReflectUtils.invokeSetter(entity, propertyName, val);
276 } 281 }
@@ -529,13 +534,18 @@ public class ExcelUtil<T> @@ -529,13 +534,18 @@ public class ExcelUtil<T>
529 Object value = getTargetValue(vo, field, attr); 534 Object value = getTargetValue(vo, field, attr);
530 String dateFormat = attr.dateFormat(); 535 String dateFormat = attr.dateFormat();
531 String readConverterExp = attr.readConverterExp(); 536 String readConverterExp = attr.readConverterExp();
  537 + String dictType = attr.dictType();
532 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) 538 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
533 { 539 {
534 cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value)); 540 cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
535 } 541 }
536 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) 542 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
537 { 543 {
538 - cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp)); 544 + cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp));
  545 + }
  546 + else if (StringUtils.isNotEmpty(dictType))
  547 + {
  548 + cell.setCellValue(convertDictByExp(dictType, Convert.toStr(value)));
539 } 549 }
540 else 550 else
541 { 551 {
@@ -667,6 +677,30 @@ public class ExcelUtil<T> @@ -667,6 +677,30 @@ public class ExcelUtil<T>
667 } 677 }
668 678
669 /** 679 /**
  680 + * 解析字典值
  681 + *
  682 + * @param dictType 字典类型
  683 + * @param dictValue 字典值
  684 + * @return 字典标签
  685 + */
  686 + public static String convertDictByExp(String dictType, String dictValue) throws Exception
  687 + {
  688 + return DictUtils.getDictLabel(dictType, dictValue);
  689 + }
  690 +
  691 + /**
  692 + * 反向解析值字典值
  693 + *
  694 + * @param dictType 字典类型
  695 + * @param dictValue 字典标签
  696 + * @return 字典值
  697 + */
  698 + public static String reverseDictByExp(String dictType, String dictLabel) throws Exception
  699 + {
  700 + return DictUtils.getDictValue(dictType, dictLabel);
  701 + }
  702 +
  703 + /**
670 * 编码文件名 704 * 编码文件名
671 */ 705 */
672 public String encodingFilename(String filename) 706 public String encodingFilename(String filename)