From ca4b62abd2560b6dcffbf3ff43da1b04427b494b Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期二, 20 一月 2026 12:36:47 +0800
Subject: [PATCH] 增加了签名和注解

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java |  203 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 200 insertions(+), 3 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
index 822c066..1a6397a 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
@@ -325,7 +325,7 @@
                     // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
 
                         // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
-                        sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
+                        sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$5"));
                      row = sheet.createRow(0);
                     row.setHeight((short) (37*20));
 
@@ -527,11 +527,18 @@
     public void fillExcelData(int index, Row row) {
 //        int startNo = index * sheetSize;
 //        int endNo = Math.min(startNo + sheetSize, list.size());
+
+        int startRow = (index == 1) ? 5 : 1;
         for (int i = 0; i < list.size(); i++) {
             if(index==1)
                 row = sheet.createRow(i + 5 );
             else
                 row = sheet.createRow(i + 1 );
+            // 绗竴姝ワ細璁㏄OI鑷姩璁$畻閫傞厤鍐呭鐨勮楂�
+         //   sheet.autoSizeRow(0);
+            // 绗簩姝ワ細鑾峰彇鑷姩璁$畻鍚庣殑琛岄珮
+
+        //    row.setHeight((short) -1); // 鍏堣涓鸿嚜鍔紝鐢盿ddCell璁$畻鍚庤鐩�
 
             // 寰楀埌瀵煎嚭瀵硅薄.
             T vo = (T) list.get(i);
@@ -542,10 +549,182 @@
                 // 璁剧疆瀹炰綋绫荤鏈夊睘鎬у彲璁块棶
                 field.setAccessible(true);
                 this.addCell(excel, row, vo, field, column++);
+
+            }
+            // 鎵归噺璋冩暣琛岄珮锛堟暟鎹~鍏呭畬鎴愬悗锛�
+            if (list.size() > 0) {
+                int lastRow = startRow + list.size() - 1;
+                batchAdjustRowHeights(sheet, startRow, lastRow);
             }
         }
     }
+    /**
+     * 鎵归噺澶勭悊琛岄珮锛堝湪鏁版嵁濉厖瀹屾垚鍚庣粺涓�璁$畻锛�
+     */
+    /**
+     * 鎵归噺澶勭悊琛岄珮锛堝湪鏁版嵁濉厖瀹屾垚鍚庣粺涓�璁$畻锛�
+     */
+    private String getCellStringValue(Cell cell) {
+        if (cell == null) return null;
 
+        switch (cell.getCellType()) {
+            case STRING:
+                return cell.getStringCellValue();
+            case NUMERIC:
+                return String.valueOf(cell.getNumericCellValue());
+            case BOOLEAN:
+                return String.valueOf(cell.getBooleanCellValue());
+            case FORMULA:
+                try {
+                    return cell.getStringCellValue();
+                } catch (Exception e) {
+                    return String.valueOf(cell.getNumericCellValue());
+                }
+            default:
+                return null;
+        }
+    }
+    private void batchAdjustRowHeights(Sheet sheet, int startRow, int endRow) {
+        Workbook workbook = sheet.getWorkbook();
+
+
+        for (int rowNum = startRow; rowNum <= endRow; rowNum++) {
+            Row row = sheet.getRow(rowNum);
+            if (row == null) continue;
+
+            int maxLinesInRow = 1; // 璁板綍璇ヨ鎵�鏈夊崟鍏冩牸涓殑鏈�澶ц鏁�
+
+            // 绗竴姝ワ細閬嶅巻璇ヨ鐨勬墍鏈夊崟鍏冩牸锛屾壘鍒版渶澶ц鏁伴渶姹�
+            for (int colNum = 0; colNum < row.getLastCellNum(); colNum++) {
+                Cell cell = row.getCell(colNum);
+                if (cell == null) continue;
+
+                // 鑾峰彇鍗曞厓鏍煎唴瀹�
+                String content = getCellStringValue(cell);
+                if (content == null || content.isEmpty()) continue;
+
+                // 鑾峰彇鍒楀
+                int columnWidth = sheet.getColumnWidth(colNum) / 256;
+                if (columnWidth <= 0) columnWidth = 10; // 榛樿鍒楀
+
+                // 璁$畻璇ュ崟鍏冩牸鍐呭闇�瑕佺殑琛屾暟
+                int contentLines = calculateContentLines(content, columnWidth);
+
+                // 鏇存柊鏈�澶ц鏁�
+                maxLinesInRow = Math.max(maxLinesInRow, contentLines);
+            }
+
+            // 绗簩姝ワ細鏍规嵁鏈�澶ц鏁拌缃楂橈紙鍏抽敭淇锛�
+            if (maxLinesInRow > 1) {
+                // 鍩虹琛岄珮锛氫竴琛屾枃鏈殑楂樺害
+                int baseHeightPerLine = 400; // 20鐐� = 400鍗曚綅锛堝缓璁�硷級
+
+                // 璁$畻鎬婚珮搴� = 琛屾暟 脳 姣忚楂樺害
+                int newHeight = maxLinesInRow * baseHeightPerLine + 600;
+
+                // 闄愬埗鏈�灏忓拰鏈�澶ч珮搴�
+                int minHeight = 300;   // 15鐐�
+                int maxHeight = 10000; // 500鐐�
+                newHeight = Math.max(minHeight, Math.min(newHeight, maxHeight));
+
+                // 璁剧疆琛岄珮
+                row.setHeight((short) newHeight);
+
+                // 鍙�夛細璁板綍璋冩暣淇℃伅鐢ㄤ簬璋冭瘯
+                log.debug("Row {} adjusted: maxLines={}, height={}",
+                        rowNum, maxLinesInRow, newHeight);
+            }
+        }
+    }
+    /**
+     * 璁$畻鏂囨湰鐨勬湁鏁堝瓧绗﹀搴�
+     */
+    private double calculateEffectiveWidth(String text) {
+        if (text == null || text.isEmpty()) {
+            return 0;
+        }
+
+        double totalWidth = 0;
+
+        for (char c : text.toCharArray()) {
+            if (isChineseChar(c)) {
+                // 涓枃瀛楃锛�1.0瀹藉害
+                totalWidth += 2.0;
+            } else if (Character.isDigit(c)) {
+                // 鏁板瓧锛�0.6瀹藉害
+                totalWidth += 0.6;
+            } else if (Character.isUpperCase(c)) {
+                // 澶у啓瀛楁瘝锛�0.7瀹藉害
+                totalWidth += 0.7;
+            } else if (Character.isLowerCase(c)) {
+                // 灏忓啓瀛楁瘝锛�0.5瀹藉害
+                totalWidth += 0.5;
+            } else if (c == '.' || c == ',') {
+                // 鐐瑰彿銆侀�楀彿锛�0.3瀹藉害
+                totalWidth += 0.3;
+            } else if (c == '-' || c == '_') {
+                // 杩炲瓧绗︺�佷笅鍒掔嚎锛�0.35瀹藉害
+                totalWidth += 0.35;
+            } else if (c == ' ') {
+                // 绌烘牸锛�0.3瀹藉害
+                totalWidth += 0.3;
+            } else if (c == '\t') {
+                // 鍒惰〃绗︼細4.0瀹藉害
+                totalWidth += 4.0;
+            } else {
+                // 鍏朵粬瀛楃锛氶粯璁�0.6瀹藉害
+                totalWidth += 0.6;
+            }
+        }
+
+        return totalWidth;
+    }
+    /**
+     * 璁$畻鍗曞厓鏍煎唴瀹规墍闇�琛屾暟锛堟洿绮剧‘鐨勭増鏈級
+     */
+    private int calculateContentLines(String content, int columnWidthChars) {
+        if (content == null || content.isEmpty() || columnWidthChars <= 0) {
+            return 1;
+        }
+
+        int totalLines = 0;
+        String[] lines = content.split("\n");
+
+        for (String line : lines) {
+            if (line.trim().isEmpty()) {
+                totalLines++; // 绌鸿涔熺畻涓�琛�
+                continue;
+            }
+
+            // 璁$畻璇ヨ闇�瑕佺殑瀛楃瀹藉害
+            // 鑰冭檻涓嫳鏂囧瓧绗﹀搴﹀樊寮�
+            double effectiveLength = 0;
+
+
+            effectiveLength += calculateEffectiveWidth(line); // 涓枃瀛楃鍗�1涓搴�
+
+
+
+            // 璁$畻闇�瑕佺殑琛屾暟
+            int linesForText = (int) Math.ceil(effectiveLength / columnWidthChars);
+            totalLines += Math.max(1, linesForText);
+        }
+
+        return Math.max(totalLines, 1);
+    }
+
+    /**
+     * 鍒ゆ柇鏄惁涓轰腑鏂囧瓧绗�
+     */
+    private boolean isChineseChar(char c) {
+        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
+        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
+                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
+                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
+                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
+                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
+                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
+    }
     /**
      * 鍒涘缓琛ㄦ牸鏍峰紡
      *
@@ -745,7 +924,14 @@
                 } else if (align == HorizontalAlignment.RIGHT) {
                     styleKey = "data3";
                 }
-                cell.setCellStyle(styles.get(styleKey));
+                System.out.println(styleKey);
+                // 鑾峰彇骞朵慨鏀规牱寮�
+                CellStyle style = styles.get(styleKey);
+                Workbook workbook = row.getSheet().getWorkbook();
+                CellStyle newStyle = workbook.createCellStyle();
+                newStyle.cloneStyleFrom(style);
+                newStyle.setWrapText(true); // 鍏抽敭锛氬惎鐢ㄨ嚜鍔ㄦ崲琛�
+                cell.setCellStyle(newStyle);
 
                 // 鐢ㄤ簬璇诲彇瀵硅薄涓殑灞炴��
                 Object value = getTargetValue(vo, field, attr);
@@ -762,6 +948,17 @@
                     // 璁剧疆鍒楃被鍨�
                     setCellVo(value, attr, cell);
                 }
+
+
+                System.out.println(row.getHeight());
+//             //   adjustRowHeightAfterSetValue(row, cell, value);
+//                int defaultRowHeight = row.getHeight() / 20; // 鍘熷琛岄珮锛堣浆鎴愮偣鏁帮級
+//                if(value==null)
+//                    value="";
+//                System.out.println(sheet.getColumnWidth(column));
+//                int contentLines = getContentLines(value.toString(), sheet.getColumnWidth(column), style); // 璁$畻鎹㈣鍚庣殑琛屾暟
+//                int newHeight = defaultRowHeight * contentLines; // 鎸夎鏁拌皟鏁磋楂橈紙鍙姞灏戦噺鍐椾綑锛�
+//                row.setHeightInPoints((short) (newHeight)); // 鎻愰珮琛岄珮锛�+2 鏄啑浣欙紝閬垮厤鍐呭琚埅鏂級
                 addStatisticsData(column, Convert.toStr(value), attr);
             }
         } catch (Exception e) {
@@ -1101,7 +1298,7 @@
            // header.setFontName("瀹嬩綋");4
            // header.setFontSize((short) 10);
             // 璁剧疆椤电湁鍐呭
-            hssfSheet.setMargin(Sheet.HeaderMargin, 1.34);
+            hssfSheet.setMargin(Sheet.HeaderMargin, 1.43);
             header.setRight("&\"瀹嬩綋,Bold\"鍏� &N 椤�   绗� &P 椤�");
         }
         // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿

--
Gitblit v1.9.1