作者 RuoYi

Excel注解新增属性comboReadDict

@@ -89,6 +89,11 @@ public @interface Excel @@ -89,6 +89,11 @@ public @interface Excel
89 public String[] combo() default {}; 89 public String[] combo() default {};
90 90
91 /** 91 /**
  92 + * 是否从字典读数据到combo,默认不读取,如读取需要设置dictType注解.
  93 + */
  94 + public boolean comboReadDict() default false;
  95 +
  96 + /**
92 * 是否需要纵向合并单元格,应对需求:含有list集合单元格) 97 * 是否需要纵向合并单元格,应对需求:含有list集合单元格)
93 */ 98 */
94 public boolean needMerge() default false; 99 public boolean needMerge() default false;
@@ -91,9 +91,10 @@ public class DictUtils @@ -91,9 +91,10 @@ public class DictUtils
91 { 91 {
92 StringBuilder propertyString = new StringBuilder(); 92 StringBuilder propertyString = new StringBuilder();
93 List<SysDictData> datas = getDictCache(dictType); 93 List<SysDictData> datas = getDictCache(dictType);
94 -  
95 - if (StringUtils.isNotNull(datas)) 94 + if (StringUtils.isNull(datas))
96 { 95 {
  96 + return StringUtils.EMPTY;
  97 + }
97 if (StringUtils.containsAny(separator, dictValue)) 98 if (StringUtils.containsAny(separator, dictValue))
98 { 99 {
99 for (SysDictData dict : datas) 100 for (SysDictData dict : datas)
@@ -118,7 +119,6 @@ public class DictUtils @@ -118,7 +119,6 @@ public class DictUtils
118 } 119 }
119 } 120 }
120 } 121 }
121 - }  
122 return StringUtils.stripEnd(propertyString.toString(), separator); 122 return StringUtils.stripEnd(propertyString.toString(), separator);
123 } 123 }
124 124
@@ -134,8 +134,11 @@ public class DictUtils @@ -134,8 +134,11 @@ public class DictUtils
134 { 134 {
135 StringBuilder propertyString = new StringBuilder(); 135 StringBuilder propertyString = new StringBuilder();
136 List<SysDictData> datas = getDictCache(dictType); 136 List<SysDictData> datas = getDictCache(dictType);
137 -  
138 - if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas)) 137 + if (StringUtils.isNull(datas))
  138 + {
  139 + return StringUtils.EMPTY;
  140 + }
  141 + if (StringUtils.containsAny(separator, dictLabel))
139 { 142 {
140 for (SysDictData dict : datas) 143 for (SysDictData dict : datas)
141 { 144 {
@@ -163,6 +166,48 @@ public class DictUtils @@ -163,6 +166,48 @@ public class DictUtils
163 } 166 }
164 167
165 /** 168 /**
  169 + * 根据字典类型获取字典所有值
  170 + *
  171 + * @param dictType 字典类型
  172 + * @return 字典值
  173 + */
  174 + public static String getDictValues(String dictType)
  175 + {
  176 + StringBuilder propertyString = new StringBuilder();
  177 + List<SysDictData> datas = getDictCache(dictType);
  178 + if (StringUtils.isNull(datas))
  179 + {
  180 + return StringUtils.EMPTY;
  181 + }
  182 + for (SysDictData dict : datas)
  183 + {
  184 + propertyString.append(dict.getDictValue()).append(SEPARATOR);
  185 + }
  186 + return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
  187 + }
  188 +
  189 + /**
  190 + * 根据字典类型获取字典所有标签
  191 + *
  192 + * @param dictType 字典类型
  193 + * @return 字典值
  194 + */
  195 + public static String getDictLabels(String dictType)
  196 + {
  197 + StringBuilder propertyString = new StringBuilder();
  198 + List<SysDictData> datas = getDictCache(dictType);
  199 + if (StringUtils.isNull(datas))
  200 + {
  201 + return StringUtils.EMPTY;
  202 + }
  203 + for (SysDictData dict : datas)
  204 + {
  205 + propertyString.append(dict.getDictLabel()).append(SEPARATOR);
  206 + }
  207 + return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
  208 + }
  209 +
  210 + /**
166 * 删除指定字典缓存 211 * 删除指定字典缓存
167 * 212 *
168 * @param key 字典键 213 * @param key 字典键
@@ -1042,17 +1042,28 @@ public class ExcelUtil<T> @@ -1042,17 +1042,28 @@ public class ExcelUtil<T>
1042 // 设置列宽 1042 // 设置列宽
1043 sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256)); 1043 sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
1044 } 1044 }
1045 - if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) 1045 + if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0 || attr.comboReadDict())
1046 { 1046 {
1047 - if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255) 1047 + String[] comboArray = attr.combo();
  1048 + if (attr.comboReadDict())
  1049 + {
  1050 + if (!sysDictMap.containsKey("combo_" + attr.dictType()))
  1051 + {
  1052 + String labels = DictUtils.getDictLabels(attr.dictType());
  1053 + sysDictMap.put("combo_" + attr.dictType(), labels);
  1054 + }
  1055 + String val = sysDictMap.get("combo_" + attr.dictType());
  1056 + comboArray = StringUtils.split(val, DictUtils.SEPARATOR);
  1057 + }
  1058 + if (comboArray.length > 15 || StringUtils.join(comboArray).length() > 255)
1048 { 1059 {
1049 // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到 1060 // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到
1050 - setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); 1061 + setXSSFValidationWithHidden(sheet, comboArray, attr.prompt(), 1, 100, column, column);
1051 } 1062 }
1052 else 1063 else
1053 { 1064 {
1054 // 提示信息或只能选择不能输入的列内容. 1065 // 提示信息或只能选择不能输入的列内容.
1055 - setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); 1066 + setPromptOrValidation(sheet, comboArray, attr.prompt(), 1, 100, column, column);
1056 } 1067 }
1057 } 1068 }
1058 } 1069 }