fei
2 天以前 5d91a329768a2a86e01e4b9b6bc6a2b939b87adb
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
@@ -325,7 +325,7 @@
                    // 如果是index为1的sheet,设置顶端标题行
                        // 将第1行(索引为0)设置为每页重复的标题行
                        sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
                        sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$5"));
                     row = sheet.createRow(0);
                    row.setHeight((short) (37*20));
@@ -532,6 +532,10 @@
                row = sheet.createRow(i + 5 );
            else
                row = sheet.createRow(i + 1 );
            // 第一步:让POI自动计算适配内容的行高
         //   sheet.autoSizeRow(0);
            // 第二步:获取自动计算后的行高
            // 得到导出对象.
            T vo = (T) list.get(i);
@@ -542,7 +546,9 @@
                // 设置实体类私有属性可访问
                field.setAccessible(true);
                this.addCell(excel, row, vo, field, column++);
            }
        }
    }
@@ -730,7 +736,7 @@
        Cell cell = null;
        try {
            // 设置行高为自动调整
            row.setHeight((short) -1);
          //  row.setHeight((short) -1);
            // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
            if (attr.isExport()) {
                // 创建cell
@@ -745,7 +751,13 @@
                } else if (align == HorizontalAlignment.RIGHT) {
                    styleKey = "data3";
                }
                cell.setCellStyle(styles.get(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 +774,9 @@
                    // 设置列类型
                    setCellVo(value, attr, cell);
                }
                adjustRowHeightAfterSetValue(row, cell, value);
                addStatisticsData(column, Convert.toStr(value), attr);
            }
        } catch (Exception e) {
@@ -769,7 +784,65 @@
        }
        return cell;
    }
    /**
     * 设置值后调整行高
     */
    private void adjustRowHeightAfterSetValue(Row row, Cell cell, Object value) {
        if (value == null) return;
        String text = value.toString();
        Sheet sheet = row.getSheet();
        // 1. 计算自动高度(基于内容)
        short autoHeight = calculateSimpleAutoHeight(text, cell);
        // 2. 获取当前行高
        short currentHeight = row.getHeight();
        if (currentHeight == -1) {
            currentHeight = sheet.getDefaultRowHeight();
        }
        // 3. 使用较大的高度(自动计算的高度或当前高度)
        short baseHeight = (short) Math.max(currentHeight, autoHeight);
        System.out.println(baseHeight+"aaaaaaaaatttttttt");
        // 4. 在基础上增加额外高度(100单位 = 5点)
        short extraHeight = 80;
        short newHeight = (short) (baseHeight + extraHeight);
        // 5. 限制最大高度
        short maxHeight = (short) 4000; // 100点
        row.setHeight((short) Math.min(maxHeight,newHeight));
    }
    /**
     * 简化的自动高度计算
     */
    private short calculateSimpleAutoHeight(String text, Cell cell) {
        if (text == null || text.isEmpty()) return 0;
        Sheet sheet = cell.getSheet();
        int colIndex = cell.getColumnIndex();
        // 获取列宽(字符数)
        int colWidthChars = sheet.getColumnWidth(colIndex) / 256;
        if (colWidthChars <= 0) colWidthChars = 10;
        // 计算文本行数
        int lines = 1;
        if (text.contains("\n")) {
            // 有显式换行
            String[] parts = text.split("\n");
            for (String part : parts) {
                lines += Math.max(1, (int) Math.ceil(part.length() * 1.5 / colWidthChars));
            }
        } else {
            // 自动换行
            lines = (int) Math.ceil(text.length() * 1.5 / colWidthChars);
        }
        // 每行高度:假设18点(360 POI单位)
        return (short) (lines * 360);
    }
    /**
     * 设置 POI HSSFSheet 单元格提示
     *
@@ -1101,7 +1174,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 页");
        }
        // 如果是index为1的sheet,设置顶端标题行