Procházet zdrojové kódy

兼容Excel下拉框内容过多无法显示的问题

RuoYi před 3 roky
rodič
revize
7a112c7317

+ 63 - 2
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -37,6 +37,7 @@ import org.apache.poi.ss.usermodel.FillPatternType;
37 37
 import org.apache.poi.ss.usermodel.Font;
38 38
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
39 39
 import org.apache.poi.ss.usermodel.IndexedColors;
40
+import org.apache.poi.ss.usermodel.Name;
40 41
 import org.apache.poi.ss.usermodel.Row;
41 42
 import org.apache.poi.ss.usermodel.Sheet;
42 43
 import org.apache.poi.ss.usermodel.VerticalAlignment;
@@ -855,8 +856,16 @@ public class ExcelUtil<T>
855 856
         }
856 857
         if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
857 858
         {
858
-            // 提示信息或只能选择不能输入的列内容.
859
-            setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
859
+            if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255)
860
+            {
861
+                // 如果下拉数大于15或字符串长度大于255,则使用一个新sheet存储,避免生成的模板下拉值获取不到
862
+                setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
863
+            }
864
+            else
865
+            {
866
+                // 提示信息或只能选择不能输入的列内容.
867
+                setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
868
+            }
860 869
         }
861 870
     }
862 871
 
@@ -956,6 +965,58 @@ public class ExcelUtil<T>
956 965
     }
957 966
 
958 967
     /**
968
+     * 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框).
969
+     * 
970
+     * @param sheet 要设置的sheet.
971
+     * @param textlist 下拉框显示的内容
972
+     * @param promptContent 提示内容
973
+     * @param firstRow 开始行
974
+     * @param endRow 结束行
975
+     * @param firstCol 开始列
976
+     * @param endCol 结束列
977
+     */
978
+    public void setXSSFValidationWithHidden(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol)
979
+    {
980
+        String hideSheetName = "combo_" + firstCol + "_" + endCol;
981
+        Sheet hideSheet = wb.createSheet(hideSheetName); // 用于存储 下拉菜单数据
982
+        for (int i = 0; i < textlist.length; i++)
983
+        {
984
+            hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]);
985
+        }
986
+        // 创建名称,可被其他单元格引用
987
+        Name name = wb.createName();
988
+        name.setNameName(hideSheetName + "_data");
989
+        name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length);
990
+        DataValidationHelper helper = sheet.getDataValidationHelper();
991
+        // 加载下拉列表内容
992
+        DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
993
+        // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
994
+        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
995
+        // 数据有效性对象
996
+        DataValidation dataValidation = helper.createValidation(constraint, regions);
997
+        if (StringUtils.isNotEmpty(promptContent))
998
+        {
999
+            // 如果设置了提示信息则鼠标放上去提示
1000
+            dataValidation.createPromptBox("", promptContent);
1001
+            dataValidation.setShowPromptBox(true);
1002
+        }
1003
+        // 处理Excel兼容性问题
1004
+        if (dataValidation instanceof XSSFDataValidation)
1005
+        {
1006
+            dataValidation.setSuppressDropDownArrow(true);
1007
+            dataValidation.setShowErrorBox(true);
1008
+        }
1009
+        else
1010
+        {
1011
+            dataValidation.setSuppressDropDownArrow(false);
1012
+        }
1013
+
1014
+        sheet.addValidationData(dataValidation);
1015
+        // 设置hiddenSheet隐藏
1016
+        wb.setSheetHidden(wb.getSheetIndex(hideSheet), true);
1017
+    }
1018
+
1019
+    /**
959 1020
      * 解析导出值 0=男,1=女,2=未知
960 1021
      *
961 1022
      * @param propertyValue 参数值