作者 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,10 +56,49 @@ public class DictUtils @@ -51,10 +56,49 @@ 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)
55 { 83 {
  84 + StringBuilder propertyString = new StringBuilder();
56 List<SysDictData> datas = getDictCache(dictType); 85 List<SysDictData> datas = getDictCache(dictType);
57 - if (StringUtils.isNotEmpty(datas)) 86 +
  87 + if (StringUtils.containsAny(separator, dictValue) && StringUtils.isNotEmpty(datas))
  88 + {
  89 + for (SysDictData dict : datas)
  90 + {
  91 + for (String value : dictValue.split(separator))
  92 + {
  93 + if (value.equals(dict.getDictValue()))
  94 + {
  95 + propertyString.append(dict.getDictLabel() + separator);
  96 + break;
  97 + }
  98 + }
  99 + }
  100 + }
  101 + else
58 { 102 {
59 for (SysDictData dict : datas) 103 for (SysDictData dict : datas)
60 { 104 {
@@ -64,8 +108,7 @@ public class DictUtils @@ -64,8 +108,7 @@ public class DictUtils
64 } 108 }
65 } 109 }
66 } 110 }
67 - }  
68 - return dictValue; 111 + return StringUtils.stripEnd(propertyString.toString(), separator);
69 } 112 }
70 113
71 /** 114 /**
@@ -73,14 +116,29 @@ public class DictUtils @@ -73,14 +116,29 @@ 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)  
79 - {  
80 - if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotEmpty(dictLabel)) 122 + public static String getDictValue(String dictType, String dictLabel, String separator)
81 { 123 {
  124 + StringBuilder propertyString = new StringBuilder();
82 List<SysDictData> datas = getDictCache(dictType); 125 List<SysDictData> datas = getDictCache(dictType);
83 - if (StringUtils.isNotEmpty(datas)) 126 +
  127 + if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas))
  128 + {
  129 + for (SysDictData dict : datas)
  130 + {
  131 + for (String label : dictLabel.split(separator))
  132 + {
  133 + if (label.equals(dict.getDictLabel()))
  134 + {
  135 + propertyString.append(dict.getDictValue() + separator);
  136 + break;
  137 + }
  138 + }
  139 + }
  140 + }
  141 + else
84 { 142 {
85 for (SysDictData dict : datas) 143 for (SysDictData dict : datas)
86 { 144 {
@@ -90,8 +148,7 @@ public class DictUtils @@ -90,8 +148,7 @@ public class DictUtils
90 } 148 }
91 } 149 }
92 } 150 }
93 - }  
94 - return dictLabel; 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 - {  
631 - try 630 + public static String convertByExp(String propertyValue, String converterExp, String separator)
632 { 631 {
  632 + StringBuilder propertyString = new StringBuilder();
633 String[] convertSource = converterExp.split(","); 633 String[] convertSource = converterExp.split(",");
634 for (String item : convertSource) 634 for (String item : convertSource)
635 { 635 {
636 String[] itemArray = item.split("="); 636 String[] itemArray = item.split("=");
637 - if (itemArray[0].equals(propertyValue)) 637 + if (StringUtils.containsAny(separator, propertyValue))
638 { 638 {
639 - return itemArray[1]; 639 + for (String value : propertyValue.split(separator))
  640 + {
  641 + if (itemArray[0].equals(value))
  642 + {
  643 + propertyString.append(itemArray[1] + separator);
  644 + break;
640 } 645 }
641 } 646 }
642 } 647 }
643 - catch (Exception e) 648 + else
  649 + {
  650 + if (itemArray[0].equals(propertyValue))
644 { 651 {
645 - throw e; 652 + return itemArray[1];
646 } 653 }
647 - return propertyValue; 654 + }
  655 + }
  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  
659 - {  
660 - try 667 + public static String reverseByExp(String propertyValue, String converterExp, String separator)
661 { 668 {
  669 + StringBuilder propertyString = new StringBuilder();
662 String[] convertSource = converterExp.split(","); 670 String[] convertSource = converterExp.split(",");
663 for (String item : convertSource) 671 for (String item : convertSource)
664 { 672 {
665 String[] itemArray = item.split("="); 673 String[] itemArray = item.split("=");
666 - if (itemArray[1].equals(propertyValue)) 674 + if (StringUtils.containsAny(separator, propertyValue))
667 { 675 {
668 - return itemArray[0]; 676 + for (String value : propertyValue.split(separator))
  677 + {
  678 + if (itemArray[1].equals(value))
  679 + {
  680 + propertyString.append(itemArray[0] + separator);
  681 + break;
669 } 682 }
670 } 683 }
671 } 684 }
672 - catch (Exception e) 685 + else
673 { 686 {
674 - throw e; 687 + if (itemArray[1].equals(propertyValue))
  688 + {
  689 + return itemArray[0];
  690 + }
675 } 691 }
676 - return propertyValue; 692 + }
  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 /**