Commit 8a076e175f6ae1815c66b00dbc9f8b0090683e1a

Authored by RuoYi
1 parent cee572f2

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

ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
... ... @@ -30,6 +30,11 @@ public @interface Excel
30 30 public String dateFormat() default "";
31 31  
32 32 /**
  33 + * 如果是字典类型,请设置字典的type值
  34 + */
  35 + public String dictType() default "";
  36 +
  37 + /**
33 38 * 读取内容转表达式 (如: 0=男,1=女,2=未知)
34 39 */
35 40 public String readConverterExp() default "";
... ... @@ -115,4 +120,4 @@ public @interface Excel
115 120 return this.value;
116 121 }
117 122 }
118   -}
  123 +}
119 124 \ No newline at end of file
... ...
ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java
... ... @@ -2,7 +2,6 @@ package com.ruoyi.common.utils;
2 2  
3 3 import java.util.Collection;
4 4 import java.util.List;
5   -
6 5 import com.ruoyi.common.constant.Constants;
7 6 import com.ruoyi.common.core.domain.entity.SysDictData;
8 7 import com.ruoyi.common.core.redis.RedisCache;
... ... @@ -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 100 public static void clearDictCache()
... ...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
... ... @@ -50,6 +50,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
50 50 import com.ruoyi.common.core.text.Convert;
51 51 import com.ruoyi.common.exception.CustomException;
52 52 import com.ruoyi.common.utils.DateUtils;
  53 +import com.ruoyi.common.utils.DictUtils;
53 54 import com.ruoyi.common.utils.StringUtils;
54 55 import com.ruoyi.common.utils.reflect.ReflectUtils;
55 56  
... ... @@ -270,7 +271,11 @@ public class ExcelUtil&lt;T&gt;
270 271 }
271 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 280 ReflectUtils.invokeSetter(entity, propertyName, val);
276 281 }
... ... @@ -529,13 +534,18 @@ public class ExcelUtil&lt;T&gt;
529 534 Object value = getTargetValue(vo, field, attr);
530 535 String dateFormat = attr.dateFormat();
531 536 String readConverterExp = attr.readConverterExp();
  537 + String dictType = attr.dictType();
532 538 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
533 539 {
534 540 cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
535 541 }
536 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 550 else
541 551 {
... ... @@ -667,6 +677,30 @@ public class ExcelUtil&lt;T&gt;
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 706 public String encodingFilename(String filename)
... ...