wwh 1 週間 前
コミット
9a92e323c5

+ 9 - 0
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/LivestockOwnershipHouseholdExcelExporter.java

@@ -124,6 +124,15 @@ public class LivestockOwnershipHouseholdExcelExporter
124 124
 
125 125
     private static void clearTemplateDataRegion(Sheet sheet, int fromRow)
126 126
     {
127
+        // 清除表体区域合并(模板合计行曾合并「序号+乡镇+村」);合计合并稍后由 mergeTotalLabelCells 重建
128
+        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--)
129
+        {
130
+            CellRangeAddress region = sheet.getMergedRegion(i);
131
+            if (region.getFirstRow() >= fromRow)
132
+            {
133
+                sheet.removeMergedRegion(i);
134
+            }
135
+        }
127 136
         int lastRow = sheet.getLastRowNum();
128 137
         for (int i = fromRow; i <= lastRow; i++)
129 138
         {

+ 9 - 0
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/LivestockProductOutputExcelExporter.java

@@ -138,6 +138,15 @@ public class LivestockProductOutputExcelExporter
138 138
 
139 139
     private static void clearTemplateDataRegion(Sheet sheet, int fromRow)
140 140
     {
141
+        // 清除表体区域合并(模板合计行曾合并「序号+乡镇+村」);合计合并稍后由 mergeTotalLabelCells 重建
142
+        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--)
143
+        {
144
+            CellRangeAddress region = sheet.getMergedRegion(i);
145
+            if (region.getFirstRow() >= fromRow)
146
+            {
147
+                sheet.removeMergedRegion(i);
148
+            }
149
+        }
141 150
         int lastRow = sheet.getLastRowNum();
142 151
         for (int i = fromRow; i <= lastRow; i++)
143 152
         {

+ 9 - 0
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/YakOutboundReportExcelExporter.java

@@ -125,6 +125,15 @@ public class YakOutboundReportExcelExporter
125 125
 
126 126
     private static void clearTemplateDataRegion(Sheet sheet, int fromRow)
127 127
     {
128
+        // 清除表体区域合并(模板合计行曾合并「序号+乡镇+村」);合计合并稍后由 mergeTotalLabelCells 重建
129
+        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--)
130
+        {
131
+            CellRangeAddress region = sheet.getMergedRegion(i);
132
+            if (region.getFirstRow() >= fromRow)
133
+            {
134
+                sheet.removeMergedRegion(i);
135
+            }
136
+        }
128 137
         int lastRow = sheet.getLastRowNum();
129 138
         for (int i = fromRow; i <= lastRow; i++)
130 139
         {

+ 59 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/LivestockOwnershipHouseholdExcelExporterTest.java

@@ -1,17 +1,22 @@
1 1
 package com.ruoyi.web.modules.industryservice.support;
2 2
 
3
+import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.junit.jupiter.api.Assertions.assertFalse;
3 5
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
4 6
 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 7
 import static org.junit.jupiter.api.Assertions.assertTrue;
6 8
 
7 9
 import java.io.ByteArrayInputStream;
10
+import java.util.ArrayList;
8 11
 import java.util.Collections;
12
+import java.util.List;
9 13
 import org.apache.poi.ss.usermodel.BorderStyle;
10 14
 import org.apache.poi.ss.usermodel.Cell;
11 15
 import org.apache.poi.ss.usermodel.Row;
12 16
 import org.apache.poi.ss.usermodel.Sheet;
13 17
 import org.apache.poi.ss.usermodel.Workbook;
14 18
 import org.apache.poi.ss.usermodel.WorkbookFactory;
19
+import org.apache.poi.ss.util.CellRangeAddress;
15 20
 import org.junit.jupiter.api.DisplayName;
16 21
 import org.junit.jupiter.api.Test;
17 22
 import com.ruoyi.web.modules.industryservice.domain.BizLivestockOwnershipHousehold;
@@ -83,4 +88,58 @@ class LivestockOwnershipHouseholdExcelExporterTest
83 88
             }
84 89
         }
85 90
     }
91
+
92
+    @Test
93
+    @DisplayName("数据行不应继承模板合计行的合并单元格")
94
+    void exportDataRowsHaveNoMergedTownVillage() throws Exception
95
+    {
96
+        List<BizLivestockOwnershipHousehold> list = new ArrayList<>();
97
+        for (int i = 0; i < 4; i++)
98
+        {
99
+            BizLivestockOwnershipHousehold row = new BizLivestockOwnershipHousehold();
100
+            row.setTownName("雅安镇");
101
+            row.setVillageDeptId(200L + i);
102
+            row.setVillageName("测试村" + i);
103
+            row.setWithLivestockHouseholdCount(10 + i);
104
+            row.setWithoutLivestockHouseholdCount(1);
105
+            list.add(row);
106
+        }
107
+        byte[] bytes = exporter.export(2026, 5, list, "admin");
108
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
109
+        {
110
+            Sheet sheet = LivestockOwnershipHouseholdExcelParser.resolveSheet(wb);
111
+            int header = LivestockOwnershipHouseholdExcelParser.detectHeaderRow(sheet);
112
+            int dataStart = header + 2;
113
+            int totalIdx = dataStart + list.size();
114
+            for (int r = dataStart; r < totalIdx; r++)
115
+            {
116
+                Row row = sheet.getRow(r);
117
+                assertNotNull(row);
118
+                assertEquals("雅安镇",
119
+                        LivestockOwnershipHouseholdExcelParser.cellString(row, LivestockOwnershipHouseholdExcelParser.COL_TOWN));
120
+                assertTrue(LivestockOwnershipHouseholdExcelParser
121
+                        .cellString(row, LivestockOwnershipHouseholdExcelParser.COL_VILLAGE).startsWith("测试村"));
122
+                for (int i = 0; i < sheet.getNumMergedRegions(); i++)
123
+                {
124
+                    CellRangeAddress region = sheet.getMergedRegion(i);
125
+                    assertFalse(region.getFirstRow() <= r && region.getLastRow() >= r
126
+                                    && region.getFirstColumn() <= LivestockOwnershipHouseholdExcelParser.COL_TOWN
127
+                                    && region.getLastColumn() >= LivestockOwnershipHouseholdExcelParser.COL_TOWN,
128
+                            "数据行 " + r + " 不应合并乡镇/村列");
129
+                }
130
+            }
131
+            boolean totalMerged = false;
132
+            for (int i = 0; i < sheet.getNumMergedRegions(); i++)
133
+            {
134
+                CellRangeAddress region = sheet.getMergedRegion(i);
135
+                if (region.getFirstRow() == totalIdx && region.getLastRow() == totalIdx
136
+                        && region.getFirstColumn() == 0
137
+                        && region.getLastColumn() == LivestockOwnershipHouseholdExcelParser.COL_VILLAGE)
138
+                {
139
+                    totalMerged = true;
140
+                }
141
+            }
142
+            assertTrue(totalMerged, "仅合计行应合并序号~村三列");
143
+        }
144
+    }
86 145
 }

+ 48 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/LivestockProductOutputExcelTest.java

@@ -1,6 +1,7 @@
1 1
 package com.ruoyi.web.modules.industryservice.support;
2 2
 
3 3
 import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.junit.jupiter.api.Assertions.assertFalse;
4 5
 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 6
 import static org.junit.jupiter.api.Assertions.assertTrue;
6 7
 
@@ -90,6 +91,53 @@ class LivestockProductOutputExcelTest
90 91
         }
91 92
     }
92 93
 
94
+    @Test
95
+    @DisplayName("数据行不应继承模板合计行的合并单元格")
96
+    void exportDataRowsHaveNoMergedTownVillage() throws Exception
97
+    {
98
+        java.util.List<BizLivestockProductOutput> list = new java.util.ArrayList<>();
99
+        for (int i = 0; i < 4; i++)
100
+        {
101
+            list.add(sample("雅安镇", "测试村" + i, "1.0", "2.0"));
102
+        }
103
+        byte[] bytes = exporter.export(2026, 5, list, "admin");
104
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
105
+        {
106
+            Sheet sheet = LivestockProductOutputExcelParser.resolveSheet(wb);
107
+            int header = LivestockProductOutputExcelParser.detectHeaderRow(sheet);
108
+            int dataStart = header + 1;
109
+            int totalIdx = dataStart + list.size();
110
+            for (int r = dataStart; r < totalIdx; r++)
111
+            {
112
+                Row row = sheet.getRow(r);
113
+                assertNotNull(row);
114
+                assertEquals("雅安镇", LivestockProductOutputExcelParser.cellString(row, LivestockProductOutputExcelParser.COL_TOWN));
115
+                assertTrue(LivestockProductOutputExcelParser.cellString(row, LivestockProductOutputExcelParser.COL_VILLAGE)
116
+                        .startsWith("测试村"));
117
+                for (int i = 0; i < sheet.getNumMergedRegions(); i++)
118
+                {
119
+                    org.apache.poi.ss.util.CellRangeAddress region = sheet.getMergedRegion(i);
120
+                    assertFalse(region.getFirstRow() <= r && region.getLastRow() >= r
121
+                                    && region.getFirstColumn() <= LivestockProductOutputExcelParser.COL_TOWN
122
+                                    && region.getLastColumn() >= LivestockProductOutputExcelParser.COL_TOWN,
123
+                            "数据行 " + r + " 不应合并乡镇/村列");
124
+                }
125
+            }
126
+            boolean totalMerged = false;
127
+            for (int i = 0; i < sheet.getNumMergedRegions(); i++)
128
+            {
129
+                org.apache.poi.ss.util.CellRangeAddress region = sheet.getMergedRegion(i);
130
+                if (region.getFirstRow() == totalIdx && region.getLastRow() == totalIdx
131
+                        && region.getFirstColumn() == 0
132
+                        && region.getLastColumn() == LivestockProductOutputExcelParser.COL_VILLAGE)
133
+                {
134
+                    totalMerged = true;
135
+                }
136
+            }
137
+            assertTrue(totalMerged, "仅合计行应合并序号~村三列");
138
+        }
139
+    }
140
+
93 141
     private static BizLivestockProductOutput sample(String town, String village, String meat, String milk)
94 142
     {
95 143
         BizLivestockProductOutput row = new BizLivestockProductOutput();

+ 49 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/YakOutboundReportExcelExporterTest.java

@@ -1,6 +1,7 @@
1 1
 package com.ruoyi.web.modules.industryservice.support;
2 2
 
3 3
 import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.junit.jupiter.api.Assertions.assertFalse;
4 5
 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 6
 import static org.junit.jupiter.api.Assertions.assertTrue;
6 7
 
@@ -92,6 +93,54 @@ class YakOutboundReportExcelExporterTest
92 93
         }
93 94
     }
94 95
 
96
+    @Test
97
+    @DisplayName("数据行不应继承模板合计行的合并单元格")
98
+    void exportDataRowsHaveNoMergedTownVillage() throws Exception
99
+    {
100
+        java.util.List<BizYakOutboundReport> list = new java.util.ArrayList<>();
101
+        for (int i = 0; i < 4; i++)
102
+        {
103
+            list.add(sample("雅安镇", "测试村" + i, 10 + i, 100 + i));
104
+        }
105
+        byte[] bytes = exporter.export(2026, 5, list, "admin");
106
+        try (org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(
107
+                new ByteArrayInputStream(bytes)))
108
+        {
109
+            org.apache.poi.ss.usermodel.Sheet sheet = YakOutboundReportExcelParser.resolveSheet(wb);
110
+            int header = YakOutboundReportExcelParser.detectHeaderRow(sheet);
111
+            int dataStart = header + 1;
112
+            int totalIdx = dataStart + list.size();
113
+            for (int r = dataStart; r < totalIdx; r++)
114
+            {
115
+                org.apache.poi.ss.usermodel.Row row = sheet.getRow(r);
116
+                assertNotNull(row);
117
+                assertEquals("雅安镇", YakOutboundReportExcelParser.cellString(row, YakOutboundReportExcelParser.COL_TOWN));
118
+                assertTrue(YakOutboundReportExcelParser.cellString(row, YakOutboundReportExcelParser.COL_VILLAGE_NAME)
119
+                        .startsWith("测试村"));
120
+                for (int i = 0; i < sheet.getNumMergedRegions(); i++)
121
+                {
122
+                    org.apache.poi.ss.util.CellRangeAddress region = sheet.getMergedRegion(i);
123
+                    assertFalse(region.getFirstRow() <= r && region.getLastRow() >= r
124
+                                    && region.getFirstColumn() <= YakOutboundReportExcelParser.COL_TOWN
125
+                                    && region.getLastColumn() >= YakOutboundReportExcelParser.COL_TOWN,
126
+                            "数据行 " + r + " 不应合并乡镇/村列");
127
+                }
128
+            }
129
+            boolean totalMerged = false;
130
+            for (int i = 0; i < sheet.getNumMergedRegions(); i++)
131
+            {
132
+                org.apache.poi.ss.util.CellRangeAddress region = sheet.getMergedRegion(i);
133
+                if (region.getFirstRow() == totalIdx && region.getLastRow() == totalIdx
134
+                        && region.getFirstColumn() == 0
135
+                        && region.getLastColumn() == YakOutboundReportExcelParser.COL_VILLAGE_NAME)
136
+                {
137
+                    totalMerged = true;
138
+                }
139
+            }
140
+            assertTrue(totalMerged, "仅合计行应合并序号~村三列");
141
+        }
142
+    }
143
+
95 144
     private static BizYakOutboundReport sample(String town, String village, int households, int yak)
96 145
     {
97 146
         BizYakOutboundReport report = new BizYakOutboundReport();