From a5edca4c62ea99418dcaa5d792947b1bab7fe8b2 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期三, 17 十二月 2025 23:08:36 +0800
Subject: [PATCH] 修改了对应代码

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetFour.java |  109 +++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 74 insertions(+), 35 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetFour.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetFour.java
index 96852e9..eb1d918 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetFour.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetFour.java
@@ -15,9 +15,14 @@
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddressList;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
-import org.apache.poi.xssf.usermodel.XSSFDataValidation;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFDataValidation;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFDataValidation;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -302,7 +307,7 @@
                         Cell cell = row.createCell(6);
 
                         // 璁剧疆鍥剧墖澶у皬鍜屼綅缃�
-                        ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2),
+                        ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2),
                                 cell.getRow().getRowNum() + 1);
                         // 璁$畻灞呬腑浣嶇疆
 //                    int col1 = 0; // 涓棿鍒�
@@ -533,6 +538,8 @@
         CellStyle style = wb.createCellStyle();
         style.setAlignment(HorizontalAlignment.CENTER);
         style.setVerticalAlignment(VerticalAlignment.CENTER);
+        // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+        style.setWrapText(true);
         style.setBorderRight(BorderStyle.THIN);
         style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
         style.setBorderLeft(BorderStyle.THIN);
@@ -547,23 +554,41 @@
         style.setFont(dataFont);
         styles.put("data", style);
 
-        style = wb.createCellStyle();
-        style.cloneStyleFrom(styles.get("data"));
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        Font headerFont = wb.createFont();
-        headerFont.setFontName("Arial");
-        headerFont.setFontHeightInPoints((short) 10);
-        headerFont.setBold(true);
-        headerFont.setColor(IndexedColors.WHITE.getIndex());
-        style.setFont(headerFont);
-        styles.put("header", style);
+        // 鍒涘缓琛ㄥご鏍峰紡鏄犲皠锛屾牴鎹瓻xcel娉ㄨВ鍔ㄦ�佺敓鎴�
+        Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
+        for (Object[] os : fields)
+        {
+            Excel excel = (Excel) os[1];
+            String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
+            if (!headerStyles.containsKey(key))
+            {
+                 style = wb.createCellStyle();
+                style.cloneStyleFrom(styles.get("data"));
+                style.setAlignment(HorizontalAlignment.CENTER);
+                style.setVerticalAlignment(VerticalAlignment.CENTER);
+                // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+                style.setWrapText(true);
+                // 鏍规嵁娉ㄨВ璁剧疆琛ㄥご鑳屾櫙鑹� - 宸叉敞閲婏紝琛ㄥご涓嶆樉绀鸿儗鏅壊
+                /*if (excel.headerBackgroundColor() != IndexedColors.WHITE) {
+                    style.setFillForegroundColor(excel.headerBackgroundColor().index);
+                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+                }*/
+                Font headerFont = wb.createFont();
+                headerFont.setFontName("Arial");
+                headerFont.setFontHeightInPoints((short) 10);
+                headerFont.setBold(false);
+                headerFont.setColor(excel.headerColor().index);
+                style.setFont(headerFont);
+                headerStyles.put(key, style);
+            }
+        }
+        styles.putAll(headerStyles);
 
         style = wb.createCellStyle();
         style.setAlignment(HorizontalAlignment.CENTER);
         style.setVerticalAlignment(VerticalAlignment.CENTER);
+        // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+        style.setWrapText(true);
         Font totalFont = wb.createFont();
         totalFont.setFontName("Arial");
         totalFont.setFontHeightInPoints((short) 10);
@@ -573,16 +598,22 @@
         style = wb.createCellStyle();
         style.cloneStyleFrom(styles.get("data"));
         style.setAlignment(HorizontalAlignment.LEFT);
+        // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+        style.setWrapText(true);
         styles.put("data1", style);
 
         style = wb.createCellStyle();
         style.cloneStyleFrom(styles.get("data"));
         style.setAlignment(HorizontalAlignment.CENTER);
+        // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+        style.setWrapText(true);
         styles.put("data2", style);
 
         style = wb.createCellStyle();
         style.cloneStyleFrom(styles.get("data"));
         style.setAlignment(HorizontalAlignment.RIGHT);
+        // 璁剧疆鍗曞厓鏍煎唴瀹硅嚜鍔ㄦ崲琛�
+        style.setWrapText(true);
         styles.put("data3", style);
 
         return styles;
@@ -597,7 +628,9 @@
         // 鍐欏叆鍒椾俊鎭�
         cell.setCellValue(attr.name());
         setDataValidation(attr, row, column);
-        cell.setCellStyle(styles.get("header"));
+        // 鏍规嵁Excel娉ㄨВ鍔ㄦ�侀�夋嫨琛ㄥご鏍峰紡
+        String key = StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor());
+        cell.setCellStyle(styles.get(key));
         return cell;
     }
 
@@ -614,7 +647,7 @@
         } else if (Excel.ColumnType.NUMERIC == attr.cellType()) {
             cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
         } else if (Excel.ColumnType.IMAGE == attr.cellType()) {
-            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
+            ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
                     cell.getRow().getRowNum() + 1);
             String imagePath = Convert.toStr(value);
             if (StringUtils.isNotEmpty(imagePath)) {
@@ -656,17 +689,17 @@
             sheet.setColumnWidth(column, 6000);
         } else {
             // 璁剧疆鍒楀
-            sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 200));
+            sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
         }
         // 濡傛灉璁剧疆浜嗘彁绀轰俊鎭垯榧犳爣鏀句笂鍘绘彁绀�.
         if (StringUtils.isNotEmpty(attr.prompt())) {
             // 杩欓噷榛樿璁句簡2-101鍒楁彁绀�.
-            setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
+            setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
         }
         // 濡傛灉璁剧疆浜哻ombo灞炴�у垯鏈垪鍙兘閫夋嫨涓嶈兘杈撳叆
         if (attr.combo().length > 0) {
             // 杩欓噷榛樿璁句簡2-101鍒楀彧鑳介�夋嫨涓嶈兘杈撳叆.
-            setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
+            setHSSFValidation(sheet, attr.combo(), 1, 100, column, column);
         }
     }
 
@@ -676,15 +709,23 @@
     public Cell addCell(Excel attr, Row row, T vo, Field field, int column) {
         Cell cell = null;
         try {
-            // 璁剧疆琛岄珮
-            row.setHeight(maxHeight);
+            // 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
+            row.setHeight((short) -1);
             // 鏍规嵁Excel涓缃儏鍐靛喅瀹氭槸鍚﹀鍑�,鏈変簺鎯呭喌闇�瑕佷繚鎸佷负绌�,甯屾湜鐢ㄦ埛濉啓杩欎竴鍒�.
             if (attr.isExport()) {
                 // 鍒涘缓cell
                 cell = row.createCell(column);
-//                attr.align();
-//                int align = attr.align().value;
-//                cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
+                // 璁剧疆鍗曞厓鏍兼牱寮�
+                HorizontalAlignment align = attr.align();
+                String styleKey = "data";
+                if (align == HorizontalAlignment.LEFT) {
+                    styleKey = "data1";
+                } else if (align == HorizontalAlignment.CENTER) {
+                    styleKey = "data2";
+                } else if (align == HorizontalAlignment.RIGHT) {
+                    styleKey = "data3";
+                }
+                cell.setCellStyle(styles.get(styleKey));
 
                 // 鐢ㄤ簬璇诲彇瀵硅薄涓殑灞炴��
                 Object value = getTargetValue(vo, field, attr);
@@ -710,7 +751,7 @@
     }
 
     /**
-     * 璁剧疆 POI XSSFSheet 鍗曞厓鏍兼彁绀�
+     * 璁剧疆 POI HSSFSheet 鍗曞厓鏍兼彁绀�
      *
      * @param sheet         琛ㄥ崟
      * @param promptTitle   鎻愮ず鏍囬
@@ -720,7 +761,7 @@
      * @param firstCol      寮�濮嬪垪
      * @param endCol        缁撴潫鍒�
      */
-    public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
+    public void setHSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
                               int firstCol, int endCol) {
         DataValidationHelper helper = sheet.getDataValidationHelper();
         DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
@@ -742,7 +783,7 @@
      * @param endCol   缁撴潫鍒�
      * @return 璁剧疆濂界殑sheet.
      */
-    public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
+    public void setHSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
         DataValidationHelper helper = sheet.getDataValidationHelper();
         // 鍔犺浇涓嬫媺鍒楄〃鍐呭
         DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
@@ -751,11 +792,9 @@
         // 鏁版嵁鏈夋晥鎬у璞�
         DataValidation dataValidation = helper.createValidation(constraint, regions);
         // 澶勭悊Excel鍏煎鎬ч棶棰�
-        if (dataValidation instanceof XSSFDataValidation) {
-            dataValidation.setSuppressDropDownArrow(true);
-            dataValidation.setShowErrorBox(true);
-        } else {
+        if (dataValidation instanceof HSSFDataValidation) {
             dataValidation.setSuppressDropDownArrow(false);
+            dataValidation.setShowErrorBox(true);
         }
 
         sheet.addValidationData(dataValidation);
@@ -952,7 +991,7 @@
      * 鍒涘缓涓�涓伐浣滅翱
      */
     public void createWorkbook() {
-        this.wb = new SXSSFWorkbook(500);
+        this.wb = new HSSFWorkbook();
     }
 
     /**

--
Gitblit v1.9.1