From 64129e1006122375d67ac6ae73f2334656c51e0a Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期日, 03 四月 2022 18:26:53 +0800
Subject: [PATCH] 修复Excel注解prompt/combo同时使用不生效问题

---
 src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java |   53 +++++++++++++----------------------------------------
 1 files changed, 13 insertions(+), 40 deletions(-)

diff --git a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
index 99118ac..c2bb182 100644
--- a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+++ b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
@@ -786,17 +786,10 @@
             // 璁剧疆鍒楀
             sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
         }
-        // 濡傛灉璁剧疆浜嗘彁绀轰俊鎭垯榧犳爣鏀句笂鍘绘彁绀�.
-        if (StringUtils.isNotEmpty(attr.prompt()))
+        if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0)
         {
-            // 杩欓噷榛樿璁句簡2-101鍒楁彁绀�.
-            setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
-        }
-        // 濡傛灉璁剧疆浜哻ombo灞炴�у垯鏈垪鍙兘閫夋嫨涓嶈兘杈撳叆
-        if (attr.combo().length > 0)
-        {
-            // 杩欓噷榛樿璁句簡2-101鍒楀彧鑳介�夋嫨涓嶈兘杈撳叆.
-            setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
+            // 鎻愮ず淇℃伅鎴栧彧鑳介�夋嫨涓嶈兘杈撳叆鐨勫垪鍐呭.
+            setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
         }
     }
 
@@ -860,48 +853,29 @@
     }
 
     /**
-     * 璁剧疆 POI XSSFSheet 鍗曞厓鏍兼彁绀�
+     * 璁剧疆 POI XSSFSheet 鍗曞厓鏍兼彁绀烘垨閫夋嫨妗�
      * 
      * @param sheet 琛ㄥ崟
-     * @param promptTitle 鎻愮ず鏍囬
+     * @param textlist 涓嬫媺妗嗘樉绀虹殑鍐呭
      * @param promptContent 鎻愮ず鍐呭
      * @param firstRow 寮�濮嬭
      * @param endRow 缁撴潫琛�
      * @param firstCol 寮�濮嬪垪
      * @param endCol 缁撴潫鍒�
      */
-    public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
+    public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
             int firstCol, int endCol)
     {
         DataValidationHelper helper = sheet.getDataValidationHelper();
-        DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
+        DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
         CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
         DataValidation dataValidation = helper.createValidation(constraint, regions);
-        dataValidation.createPromptBox(promptTitle, promptContent);
-        dataValidation.setShowPromptBox(true);
-        sheet.addValidationData(dataValidation);
-    }
-
-    /**
-     * 璁剧疆鏌愪簺鍒楃殑鍊煎彧鑳借緭鍏ラ鍒剁殑鏁版嵁,鏄剧ず涓嬫媺妗�.
-     * 
-     * @param sheet 瑕佽缃殑sheet.
-     * @param textlist 涓嬫媺妗嗘樉绀虹殑鍐呭
-     * @param firstRow 寮�濮嬭
-     * @param endRow 缁撴潫琛�
-     * @param firstCol 寮�濮嬪垪
-     * @param endCol 缁撴潫鍒�
-     * @return 璁剧疆濂界殑sheet.
-     */
-    public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol)
-    {
-        DataValidationHelper helper = sheet.getDataValidationHelper();
-        // 鍔犺浇涓嬫媺鍒楄〃鍐呭
-        DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
-        // 璁剧疆鏁版嵁鏈夋晥鎬у姞杞藉湪鍝釜鍗曞厓鏍间笂,鍥涗釜鍙傛暟鍒嗗埆鏄細璧峰琛屻�佺粓姝㈣銆佽捣濮嬪垪銆佺粓姝㈠垪
-        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
-        // 鏁版嵁鏈夋晥鎬у璞�
-        DataValidation dataValidation = helper.createValidation(constraint, regions);
+        if (StringUtils.isNotEmpty(promptContent))
+        {
+            // 濡傛灉璁剧疆浜嗘彁绀轰俊鎭垯榧犳爣鏀句笂鍘绘彁绀�
+            dataValidation.createPromptBox("", promptContent);
+            dataValidation.setShowPromptBox(true);
+        }
         // 澶勭悊Excel鍏煎鎬ч棶棰�
         if (dataValidation instanceof XSSFDataValidation)
         {
@@ -912,7 +886,6 @@
         {
             dataValidation.setSuppressDropDownArrow(false);
         }
-
         sheet.addValidationData(dataValidation);
     }
 

--
Gitblit v1.9.3