作者 RuoYi

Excel支持分割字符串组内容

@@ -13,7 +13,7 @@ ruoyi: @@ -13,7 +13,7 @@ ruoyi:
13 # 获取ip地址开关 13 # 获取ip地址开关
14 addressEnabled: false 14 addressEnabled: false
15 # 验证码类型 math 数组计算 char 字符验证 15 # 验证码类型 math 数组计算 char 字符验证
16 - captchaType: char 16 + captchaType: math
17 17
18 # 开发环境配置 18 # 开发环境配置
19 server: 19 server:
@@ -40,6 +40,11 @@ public @interface Excel @@ -40,6 +40,11 @@ public @interface Excel
40 public String readConverterExp() default ""; 40 public String readConverterExp() default "";
41 41
42 /** 42 /**
  43 + * 分隔符,读取字符串组内容
  44 + */
  45 + public String separator() default ",";
  46 +
  47 + /**
43 * 导出类型(0数字 1字符串) 48 * 导出类型(0数字 1字符串)
44 */ 49 */
45 public ColumnType cellType() default ColumnType.STRING; 50 public ColumnType cellType() default ColumnType.STRING;
@@ -15,6 +15,11 @@ import com.ruoyi.common.utils.spring.SpringUtils; @@ -15,6 +15,11 @@ import com.ruoyi.common.utils.spring.SpringUtils;
15 public class DictUtils 15 public class DictUtils
16 { 16 {
17 /** 17 /**
  18 + * 分隔符
  19 + */
  20 + public static final String SEPARATOR = ",";
  21 +
  22 + /**
18 * 设置字典缓存 23 * 设置字典缓存
19 * 24 *
20 * @param key 参数键 25 * @param key 参数键
@@ -51,21 +56,59 @@ public class DictUtils @@ -51,21 +56,59 @@ public class DictUtils
51 */ 56 */
52 public static String getDictLabel(String dictType, String dictValue) 57 public static String getDictLabel(String dictType, String dictValue)
53 { 58 {
54 - if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictValue)) 59 + return getDictLabel(dictType, dictValue, SEPARATOR);
  60 + }
  61 +
  62 + /**
  63 + * 根据字典类型和字典标签获取字典值
  64 + *
  65 + * @param dictType 字典类型
  66 + * @param dictLabel 字典标签
  67 + * @return 字典值
  68 + */
  69 + public static String getDictValue(String dictType, String dictLabel)
  70 + {
  71 + return getDictValue(dictType, dictLabel, SEPARATOR);
  72 + }
  73 +
  74 + /**
  75 + * 根据字典类型和字典值获取字典标签
  76 + *
  77 + * @param dictType 字典类型
  78 + * @param dictValue 字典值
  79 + * @param separator 分隔符
  80 + * @return 字典标签
  81 + */
  82 + public static String getDictLabel(String dictType, String dictValue, String separator)
  83 + {
  84 + StringBuilder propertyString = new StringBuilder();
  85 + List<SysDictData> datas = getDictCache(dictType);
  86 +
  87 + if (StringUtils.containsAny(separator, dictValue) && StringUtils.isNotEmpty(datas))
55 { 88 {
56 - List<SysDictData> datas = getDictCache(dictType);  
57 - if (StringUtils.isNotEmpty(datas)) 89 + for (SysDictData dict : datas)
58 { 90 {
59 - for (SysDictData dict : datas) 91 + for (String value : dictValue.split(separator))
60 { 92 {
61 - if (dictValue.equals(dict.getDictValue())) 93 + if (value.equals(dict.getDictValue()))
62 { 94 {
63 - return dict.getDictLabel(); 95 + propertyString.append(dict.getDictLabel() + separator);
  96 + break;
64 } 97 }
65 } 98 }
66 } 99 }
67 } 100 }
68 - return dictValue; 101 + else
  102 + {
  103 + for (SysDictData dict : datas)
  104 + {
  105 + if (dictValue.equals(dict.getDictValue()))
  106 + {
  107 + return dict.getDictLabel();
  108 + }
  109 + }
  110 + }
  111 + return StringUtils.stripEnd(propertyString.toString(), separator);
69 } 112 }
70 113
71 /** 114 /**
@@ -73,25 +116,39 @@ public class DictUtils @@ -73,25 +116,39 @@ public class DictUtils
73 * 116 *
74 * @param dictType 字典类型 117 * @param dictType 字典类型
75 * @param dictLabel 字典标签 118 * @param dictLabel 字典标签
  119 + * @param separator 分隔符
76 * @return 字典值 120 * @return 字典值
77 */ 121 */
78 - public static String getDictValue(String dictType, String dictLabel) 122 + public static String getDictValue(String dictType, String dictLabel, String separator)
79 { 123 {
80 - if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel)) 124 + StringBuilder propertyString = new StringBuilder();
  125 + List<SysDictData> datas = getDictCache(dictType);
  126 +
  127 + if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas))
81 { 128 {
82 - List<SysDictData> datas = getDictCache(dictType);  
83 - if (StringUtils.isNotEmpty(datas)) 129 + for (SysDictData dict : datas)
84 { 130 {
85 - for (SysDictData dict : datas) 131 + for (String label : dictLabel.split(separator))
86 { 132 {
87 - if (dictLabel.equals(dict.getDictLabel())) 133 + if (label.equals(dict.getDictLabel()))
88 { 134 {
89 - return dict.getDictValue(); 135 + propertyString.append(dict.getDictValue() + separator);
  136 + break;
90 } 137 }
91 } 138 }
92 } 139 }
93 } 140 }
94 - return dictLabel; 141 + else
  142 + {
  143 + for (SysDictData dict : datas)
  144 + {
  145 + if (dictLabel.equals(dict.getDictLabel()))
  146 + {
  147 + return dict.getDictValue();
  148 + }
  149 + }
  150 + }
  151 + return StringUtils.stripEnd(propertyString.toString(), separator);
95 } 152 }
96 153
97 /** 154 /**
@@ -271,11 +271,11 @@ public class ExcelUtil<T> @@ -271,11 +271,11 @@ public class ExcelUtil<T>
271 } 271 }
272 else if (StringUtils.isNotEmpty(attr.readConverterExp())) 272 else if (StringUtils.isNotEmpty(attr.readConverterExp()))
273 { 273 {
274 - val = reverseByExp(Convert.toStr(val), attr.readConverterExp()); 274 + val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
275 } 275 }
276 else if (StringUtils.isNotEmpty(attr.dictType())) 276 else if (StringUtils.isNotEmpty(attr.dictType()))
277 { 277 {
278 - val = reverseDictByExp(attr.dictType(), Convert.toStr(val)); 278 + val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
279 } 279 }
280 ReflectUtils.invokeSetter(entity, propertyName, val); 280 ReflectUtils.invokeSetter(entity, propertyName, val);
281 } 281 }
@@ -534,6 +534,7 @@ public class ExcelUtil<T> @@ -534,6 +534,7 @@ public class ExcelUtil<T>
534 Object value = getTargetValue(vo, field, attr); 534 Object value = getTargetValue(vo, field, attr);
535 String dateFormat = attr.dateFormat(); 535 String dateFormat = attr.dateFormat();
536 String readConverterExp = attr.readConverterExp(); 536 String readConverterExp = attr.readConverterExp();
  537 + String separator = attr.separator();
537 String dictType = attr.dictType(); 538 String dictType = attr.dictType();
538 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) 539 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
539 { 540 {
@@ -541,11 +542,11 @@ public class ExcelUtil<T> @@ -541,11 +542,11 @@ public class ExcelUtil<T>
541 } 542 }
542 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) 543 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
543 { 544 {
544 - cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp)); 545 + cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
545 } 546 }
546 else if (StringUtils.isNotEmpty(dictType)) 547 else if (StringUtils.isNotEmpty(dictType))
547 { 548 {
548 - cell.setCellValue(convertDictByExp(dictType, Convert.toStr(value))); 549 + cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator));
549 } 550 }
550 else 551 else
551 { 552 {
@@ -623,28 +624,36 @@ public class ExcelUtil<T> @@ -623,28 +624,36 @@ public class ExcelUtil<T>
623 * 624 *
624 * @param propertyValue 参数值 625 * @param propertyValue 参数值
625 * @param converterExp 翻译注解 626 * @param converterExp 翻译注解
  627 + * @param separator 分隔符
626 * @return 解析后值 628 * @return 解析后值
627 - * @throws Exception  
628 */ 629 */
629 - public static String convertByExp(String propertyValue, String converterExp) throws Exception 630 + public static String convertByExp(String propertyValue, String converterExp, String separator)
630 { 631 {
631 - try 632 + StringBuilder propertyString = new StringBuilder();
  633 + String[] convertSource = converterExp.split(",");
  634 + for (String item : convertSource)
632 { 635 {
633 - String[] convertSource = converterExp.split(",");  
634 - for (String item : convertSource) 636 + String[] itemArray = item.split("=");
  637 + if (StringUtils.containsAny(separator, propertyValue))
  638 + {
  639 + for (String value : propertyValue.split(separator))
  640 + {
  641 + if (itemArray[0].equals(value))
  642 + {
  643 + propertyString.append(itemArray[1] + separator);
  644 + break;
  645 + }
  646 + }
  647 + }
  648 + else
635 { 649 {
636 - String[] itemArray = item.split("=");  
637 if (itemArray[0].equals(propertyValue)) 650 if (itemArray[0].equals(propertyValue))
638 { 651 {
639 return itemArray[1]; 652 return itemArray[1];
640 } 653 }
641 } 654 }
642 } 655 }
643 - catch (Exception e)  
644 - {  
645 - throw e;  
646 - }  
647 - return propertyValue; 656 + return StringUtils.stripEnd(propertyString.toString(), separator);
648 } 657 }
649 658
650 /** 659 /**
@@ -652,52 +661,62 @@ public class ExcelUtil<T> @@ -652,52 +661,62 @@ public class ExcelUtil<T>
652 * 661 *
653 * @param propertyValue 参数值 662 * @param propertyValue 参数值
654 * @param converterExp 翻译注解 663 * @param converterExp 翻译注解
  664 + * @param separator 分隔符
655 * @return 解析后值 665 * @return 解析后值
656 - * @throws Exception  
657 */ 666 */
658 - public static String reverseByExp(String propertyValue, String converterExp) throws Exception 667 + public static String reverseByExp(String propertyValue, String converterExp, String separator)
659 { 668 {
660 - try 669 + StringBuilder propertyString = new StringBuilder();
  670 + String[] convertSource = converterExp.split(",");
  671 + for (String item : convertSource)
661 { 672 {
662 - String[] convertSource = converterExp.split(",");  
663 - for (String item : convertSource) 673 + String[] itemArray = item.split("=");
  674 + if (StringUtils.containsAny(separator, propertyValue))
  675 + {
  676 + for (String value : propertyValue.split(separator))
  677 + {
  678 + if (itemArray[1].equals(value))
  679 + {
  680 + propertyString.append(itemArray[0] + separator);
  681 + break;
  682 + }
  683 + }
  684 + }
  685 + else
664 { 686 {
665 - String[] itemArray = item.split("=");  
666 if (itemArray[1].equals(propertyValue)) 687 if (itemArray[1].equals(propertyValue))
667 { 688 {
668 return itemArray[0]; 689 return itemArray[0];
669 } 690 }
670 } 691 }
671 } 692 }
672 - catch (Exception e)  
673 - {  
674 - throw e;  
675 - }  
676 - return propertyValue; 693 + return StringUtils.stripEnd(propertyString.toString(), separator);
677 } 694 }
678 - 695 +
679 /** 696 /**
680 * 解析字典值 697 * 解析字典值
681 * 698 *
682 - * @param dictType 字典类型  
683 * @param dictValue 字典值 699 * @param dictValue 字典值
  700 + * @param dictType 字典类型
  701 + * @param separator 分隔符
684 * @return 字典标签 702 * @return 字典标签
685 */ 703 */
686 - public static String convertDictByExp(String dictType, String dictValue) throws Exception 704 + public static String convertDictByExp(String dictValue, String dictType, String separator)
687 { 705 {
688 - return DictUtils.getDictLabel(dictType, dictValue); 706 + return DictUtils.getDictLabel(dictType, dictValue, separator);
689 } 707 }
690 708
691 /** 709 /**
692 * 反向解析值字典值 710 * 反向解析值字典值
693 * 711 *
  712 + * @param dictLabel 字典标签
694 * @param dictType 字典类型 713 * @param dictType 字典类型
695 - * @param dictValue 字典标签 714 + * @param separator 分隔符
696 * @return 字典值 715 * @return 字典值
697 */ 716 */
698 - public static String reverseDictByExp(String dictType, String dictLabel) throws Exception 717 + public static String reverseDictByExp(String dictLabel, String dictType, String separator)
699 { 718 {
700 - return DictUtils.getDictValue(dictType, dictLabel); 719 + return DictUtils.getDictValue(dictType, dictLabel, separator);
701 } 720 }
702 721
703 /** 722 /**