From 08ef9ccfeaef1d172d0632b070acadb235ee11b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E5=B1=95=E8=88=AA?= <2426745133@qq.com> Date: Tue, 18 Nov 2025 15:07:54 +0800 Subject: [PATCH] =?UTF-8?q?11-18-=E5=88=9D=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/BusProjectServiceImpl.java | 151 ++++++++++++------ 1 file changed, 105 insertions(+), 46 deletions(-) diff --git a/xinnengyuan/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/project/service/impl/BusProjectServiceImpl.java b/xinnengyuan/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/project/service/impl/BusProjectServiceImpl.java index ad29b4c4..59b25dd8 100644 --- a/xinnengyuan/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/project/service/impl/BusProjectServiceImpl.java +++ b/xinnengyuan/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/project/service/impl/BusProjectServiceImpl.java @@ -1770,7 +1770,7 @@ public class BusProjectServiceImpl extends ServiceImpl mergedRegions = sheet.getMergedRegions(); + boolean hasOverlap = false; + for (CellRangeAddress existingRegion : mergedRegions) { if (regionsOverlap(newRegion, existingRegion)) { + // 如果是完全相同的区域,跳过(可能是重复设置) + if (newRegion.equals(existingRegion)) { + System.out.println("跳过重复的合并区域: " + newRegion.formatAsString()); + return; + } System.err.println("跳过重叠的合并区域: " + newRegion.formatAsString() + " 与 " + existingRegion.formatAsString()); - return; + hasOverlap = true; + break; } } - try { - sheet.addMergedRegion(newRegion); - System.out.println("成功添加合并区域: " + newRegion.formatAsString()); - } catch (IllegalStateException e) { - System.err.println("添加合并区域失败: " + newRegion.formatAsString() + " - " + e.getMessage()); + if (!hasOverlap) { + try { + sheet.addMergedRegion(newRegion); + System.out.println("成功添加合并区域: " + newRegion.formatAsString()); + } catch (IllegalStateException e) { + System.err.println("添加合并区域失败: " + newRegion.formatAsString() + " - " + e.getMessage()); + } } } + /** + * 自动调整行高以适应内容 + */ + private static void autoAdjustRowHeights(Sheet sheet) { + for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) { + Row row = sheet.getRow(rowNum); + if (row != null) { + // 计算该行中所有单元格的最大行数 + int maxLines = 1; + for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { + Cell cell = row.getCell(cellNum); + if (cell != null && cell.getStringCellValue() != null) { + String cellValue = cell.getStringCellValue(); + // 根据内容长度和列宽估算行数 + int estimatedLines = estimateLineCount(cellValue, sheet.getColumnWidth(cellNum)); + maxLines = Math.max(maxLines, estimatedLines); + } + } + + // 设置行高(每行约15点) + float rowHeight = Math.max(20f, maxLines * 15f); // 最小高度20,根据行数调整 + row.setHeightInPoints(rowHeight); + } + } + } + + /** + * 估算文本在指定列宽下的行数 + */ + private static int estimateLineCount(String text, int columnWidth) { + if (text == null || text.isEmpty()) { + return 1; + } + + // 估算每个字符的宽度(近似值) + double charWidth = 256; // Excel中字符宽度的近似值 + double availableWidth = columnWidth; + + String[] lines = text.split("\n"); + int totalLines = 0; + + for (String line : lines) { + if (line.isEmpty()) { + totalLines++; + continue; + } + + // 估算该行需要的行数 + double lineWidth = line.length() * charWidth; + int linesNeeded = (int) Math.ceil(lineWidth / availableWidth); + totalLines += Math.max(1, linesNeeded); + } + + return Math.max(1, totalLines); + } + /** * 创建空单元格样式(带边框) */ @@ -2448,39 +2529,17 @@ public class BusProjectServiceImpl extends ServiceImpl