Explorar el Código

Excel注解新增属性comboReadDict

RuoYi hace 10 meses
padre
commit
bc7a607033

+ 5 - 0
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java

@@ -88,6 +88,11 @@ public @interface Excel
      */
     public String[] combo() default {};
 
+    /**
+     * 是否从字典读数据到combo,默认不读取,如读取需要设置dictType注解.
+     */
+    public boolean comboReadDict() default false;
+
     /**
      * 是否需要纵向合并单元格,应对需求:含有list集合单元格)
      */

+ 63 - 18
ruoyi-common/src/main/java/com/ruoyi/common/utils/DictUtils.java

@@ -91,31 +91,31 @@ public class DictUtils
     {
         StringBuilder propertyString = new StringBuilder();
         List<SysDictData> datas = getDictCache(dictType);
-
-        if (StringUtils.isNotNull(datas))
+        if (StringUtils.isNull(datas))
+        {
+            return StringUtils.EMPTY;
+        }
+        if (StringUtils.containsAny(separator, dictValue))
         {
-            if (StringUtils.containsAny(separator, dictValue))
+            for (SysDictData dict : datas)
             {
-                for (SysDictData dict : datas)
+                for (String value : dictValue.split(separator))
                 {
-                    for (String value : dictValue.split(separator))
+                    if (value.equals(dict.getDictValue()))
                     {
-                        if (value.equals(dict.getDictValue()))
-                        {
-                            propertyString.append(dict.getDictLabel()).append(separator);
-                            break;
-                        }
+                        propertyString.append(dict.getDictLabel()).append(separator);
+                        break;
                     }
                 }
             }
-            else
+        }
+        else
+        {
+            for (SysDictData dict : datas)
             {
-                for (SysDictData dict : datas)
+                if (dictValue.equals(dict.getDictValue()))
                 {
-                    if (dictValue.equals(dict.getDictValue()))
-                    {
-                        return dict.getDictLabel();
-                    }
+                    return dict.getDictLabel();
                 }
             }
         }
@@ -134,8 +134,11 @@ public class DictUtils
     {
         StringBuilder propertyString = new StringBuilder();
         List<SysDictData> datas = getDictCache(dictType);
-
-        if (StringUtils.containsAny(separator, dictLabel) && StringUtils.isNotEmpty(datas))
+        if (StringUtils.isNull(datas))
+        {
+            return StringUtils.EMPTY;
+        }
+        if (StringUtils.containsAny(separator, dictLabel))
         {
             for (SysDictData dict : datas)
             {
@@ -162,6 +165,48 @@ public class DictUtils
         return StringUtils.stripEnd(propertyString.toString(), separator);
     }
 
+    /**
+     * 根据字典类型获取字典所有值
+     *
+     * @param dictType 字典类型
+     * @return 字典值
+     */
+    public static String getDictValues(String dictType)
+    {
+        StringBuilder propertyString = new StringBuilder();
+        List<SysDictData> datas = getDictCache(dictType);
+        if (StringUtils.isNull(datas))
+        {
+            return StringUtils.EMPTY;
+        }
+        for (SysDictData dict : datas)
+        {
+            propertyString.append(dict.getDictValue()).append(SEPARATOR);
+        }
+        return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
+    }
+
+    /**
+     * 根据字典类型获取字典所有标签
+     *
+     * @param dictType 字典类型
+     * @return 字典值
+     */
+    public static String getDictLabels(String dictType)
+    {
+        StringBuilder propertyString = new StringBuilder();
+        List<SysDictData> datas = getDictCache(dictType);
+        if (StringUtils.isNull(datas))
+        {
+            return StringUtils.EMPTY;
+        }
+        for (SysDictData dict : datas)
+        {
+            propertyString.append(dict.getDictLabel()).append(SEPARATOR);
+        }
+        return StringUtils.stripEnd(propertyString.toString(), SEPARATOR);
+    }
+
     /**
      * 删除指定字典缓存
      * 

+ 15 - 4
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java

@@ -1042,17 +1042,28 @@ public class ExcelUtil<T>
             // 设置列宽
             sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
         }
-        if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
+        if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0 || attr.comboReadDict())
         {
-            if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255)
+            String[] comboArray = attr.combo();
+            if (attr.comboReadDict())
+            {
+                if (!sysDictMap.containsKey("combo_" + attr.dictType()))
+                {
+                    String labels = DictUtils.getDictLabels(attr.dictType());
+                    sysDictMap.put("combo_" + attr.dictType(), labels);
+                }
+                String val = sysDictMap.get("combo_" + attr.dictType());
+                comboArray = StringUtils.split(val, DictUtils.SEPARATOR);
+            }
+            if (comboArray.length > 15 || StringUtils.join(comboArray).length() > 255)
             {
                 // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到
-                setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
+                setXSSFValidationWithHidden(sheet, comboArray, attr.prompt(), 1, 100, column, column);
             }
             else
             {
                 // 提示信息或只能选择不能输入的列内容.
-                setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
+                setPromptOrValidation(sheet, comboArray, attr.prompt(), 1, 100, column, column);
             }
         }
     }