From 487bbcd45afcc288c27784962d3b40d5015bcd12 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 15 八月 2025 15:33:07 +0800
Subject: [PATCH] yys  修改导入导出

---
 src/main/java/com/ruoyi/common/utils/excel/ExcelUtils.java |   35 ++++++++++++++++++++++++++++++++---
 1 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/ruoyi/common/utils/excel/ExcelUtils.java b/src/main/java/com/ruoyi/common/utils/excel/ExcelUtils.java
index d1c23ed..230bb77 100644
--- a/src/main/java/com/ruoyi/common/utils/excel/ExcelUtils.java
+++ b/src/main/java/com/ruoyi/common/utils/excel/ExcelUtils.java
@@ -843,18 +843,47 @@
                 } else {
                     merge = false;
                     if (x1 > 0) {
-                        sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y));
+                        // 妫�鏌ユ槸鍚︿笌鐜版湁鍚堝苟鍖哄煙閲嶅彔锛屽鏋滄湁鍒欏垹闄ゆ棫鍖哄煙锛屽啀鍚堝苟鏂板尯鍩�
+                        CellRangeAddress newRegion = new CellRangeAddress((x1 - 1), x2, y, y);
+                        handleOverlappingRegions(sheet, newRegion);
                     }
                     x1 = 0;
                     x2 = 0;
                 }
             }
             if (x1 > 0) {
-                sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y));
+                // 妫�鏌ユ槸鍚︿笌鐜版湁鍚堝苟鍖哄煙閲嶅彔锛屽鏋滄湁鍒欏垹闄ゆ棫鍖哄煙锛屽啀鍚堝苟鏂板尯鍩�
+                CellRangeAddress newRegion = new CellRangeAddress((x1 - 1), x2, y, y);
+                handleOverlappingRegions(sheet, newRegion);
             }
         }
     }
- 
+
+    /**
+     * 妫�鏌ユ槸鍚︿笌鐜版湁鍚堝苟鍖哄煙閲嶅彔锛屽鏋滄湁鍒欏垹闄ゆ棫鍖哄煙锛屽啀鍚堝苟鏂板尯鍩�
+     * @param sheet Excel宸ヤ綔琛�
+     * @param newRegion 鏂扮殑鍚堝苟鍖哄煙
+     */
+    private static void handleOverlappingRegions(Sheet sheet, CellRangeAddress newRegion) {
+        // 绉婚櫎閲嶅彔鐨勭幇鏈夊尯鍩�
+        List<CellRangeAddress> overlappingRegions = new ArrayList<>();
+        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
+            CellRangeAddress existingRegion = sheet.getMergedRegion(i);
+            if (newRegion.intersects(existingRegion)) {
+                overlappingRegions.add(existingRegion);
+                sheet.removeMergedRegion(i);
+            }
+        }
+        // 鍚堝苟鍒版柊鍖哄煙锛堣繖閲岀畝鍗曞湴灏嗗畠浠坊鍔犲埌鏂扮殑鍚堝苟鍖哄煙鍒楄〃涓紝瀹為檯搴旂敤涓彲鑳介渶瑕佹洿澶嶆潅鐨勯�昏緫鏉ョ湡姝b�滃悎骞垛�濆尯鍩燂級
+        // 娉ㄦ剰锛歅OI鐨凜ellRangeAddress娌℃湁鎻愪緵鐩存帴鍚堝苟涓や釜鍖哄煙鐨勬柟娉曪紝鎵�浠ヨ繖閲屽彧鏄ず渚�
+        // 濡傛灉闇�瑕佺湡姝g殑鍚堝苟锛屽彲鑳介渶瑕佽嚜瀹氫箟閫昏緫鏉ヨ绠楁柊鐨勮竟鐣�
+        for (CellRangeAddress overlappingRegion : overlappingRegions) {
+            sheet.addMergedRegion(overlappingRegion);
+        }
+        sheet.addMergedRegion(newRegion);
+    }
+
+
     private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
         response.setCharacterEncoding("utf-8");

--
Gitblit v1.9.3