wwh hai 1 semana
pai
achega
3ab5ad1e8b

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

@@ -79,6 +79,8 @@ public class YakOutboundReportExcelExporter
79 79
             updateTitleYearMonth(sheet, statYear, statMonth);
80 80
             updateFiller(sheet, fillerName);
81 81
             int dataStartRow = headerRowIndex + 1;
82
+            CellStyle[] dataRowStyles = captureDataRowStyles(sheet, dataStartRow);
83
+            Float dataRowHeight = captureRowHeight(sheet, dataStartRow);
82 84
             clearTemplateDataRegion(sheet, dataStartRow);
83 85
             Totals totals = new Totals();
84 86
             int rowIndex = dataStartRow;
@@ -96,6 +98,7 @@ public class YakOutboundReportExcelExporter
96 98
                     setIntegerCell(row, COL_SEQ, seq++);
97 99
                     setStringCell(row, YakOutboundReportExcelParser.COL_TOWN, report.getTownName());
98 100
                     fillDataRow(row, report);
101
+                    applyDataRowStyles(row, dataRowStyles, dataRowHeight);
99 102
                     if (report.getVillageDeptId() != null || report.getYakOutboundCount() != null
100 103
                             || report.getFarmerHouseholdCount() != null)
101 104
                     {
@@ -107,8 +110,9 @@ public class YakOutboundReportExcelExporter
107 110
             Row totalRow = getOrCreateRow(sheet, rowIndex);
108 111
             clearDataCells(totalRow);
109 112
             setStringCell(totalRow, COL_SEQ, "合计");
110
-            mergeTotalLabelCells(sheet, rowIndex);
111 113
             totals.writeToRow(totalRow);
114
+            applyDataRowStyles(totalRow, dataRowStyles, dataRowHeight);
115
+            mergeTotalLabelCells(sheet, rowIndex);
112 116
             removeRowsAfter(sheet, rowIndex);
113 117
             workbook.write(out);
114 118
             return out.toByteArray();
@@ -346,6 +350,62 @@ public class YakOutboundReportExcelExporter
346 350
         return row;
347 351
     }
348 352
 
353
+    /** 从模板样例数据行捕获各列样式,供数据行与合计行复用(避免第 3 行起/合计行丢边框) */
354
+    private static CellStyle[] captureDataRowStyles(Sheet sheet, int styleSourceRow)
355
+    {
356
+        CellStyle[] styles = new CellStyle[YakOutboundReportExcelParser.COL_LAST + 1];
357
+        Row src = sheet.getRow(styleSourceRow);
358
+        if (src == null)
359
+        {
360
+            return styles;
361
+        }
362
+        for (int col = 0; col < styles.length; col++)
363
+        {
364
+            Cell cell = src.getCell(col);
365
+            if (cell != null)
366
+            {
367
+                styles[col] = cell.getCellStyle();
368
+            }
369
+        }
370
+        return styles;
371
+    }
372
+
373
+    private static Float captureRowHeight(Sheet sheet, int styleSourceRow)
374
+    {
375
+        Row src = sheet.getRow(styleSourceRow);
376
+        if (src == null)
377
+        {
378
+            return null;
379
+        }
380
+        return src.getHeightInPoints();
381
+    }
382
+
383
+    private static void applyDataRowStyles(Row row, CellStyle[] styles, Float rowHeight)
384
+    {
385
+        if (row == null || styles == null)
386
+        {
387
+            return;
388
+        }
389
+        if (rowHeight != null && rowHeight > 0)
390
+        {
391
+            row.setHeightInPoints(rowHeight);
392
+        }
393
+        for (int col = 0; col < styles.length; col++)
394
+        {
395
+            CellStyle style = styles[col];
396
+            if (style == null)
397
+            {
398
+                continue;
399
+            }
400
+            Cell cell = row.getCell(col);
401
+            if (cell == null)
402
+            {
403
+                cell = row.createCell(col);
404
+            }
405
+            cell.setCellStyle(style);
406
+        }
407
+    }
408
+
349 409
     private static void fillDataRow(Row row, BizYakOutboundReport report)
350 410
     {
351 411
         setStringCell(row, YakOutboundReportExcelParser.COL_VILLAGE_NAME, report.getVillageName());

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

@@ -2,6 +2,7 @@ package com.ruoyi.web.modules.industryservice.support;
2 2
 
3 3
 import static org.junit.jupiter.api.Assertions.assertEquals;
4 4
 import static org.junit.jupiter.api.Assertions.assertFalse;
5
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
5 6
 import static org.junit.jupiter.api.Assertions.assertNotNull;
6 7
 import static org.junit.jupiter.api.Assertions.assertTrue;
7 8
 
@@ -141,6 +142,47 @@ class YakOutboundReportExcelExporterTest
141 142
         }
142 143
     }
143 144
 
145
+    @Test
146
+    @DisplayName("第3数据行及合计行复用模板数据行边框样式")
147
+    void exportThirdDataRowAndTotalKeepBorders() throws Exception
148
+    {
149
+        java.util.List<BizYakOutboundReport> list = new java.util.ArrayList<>();
150
+        for (int i = 0; i < 3; i++)
151
+        {
152
+            list.add(sample("雅安镇", "测试村" + i, 10 + i, 100 + i));
153
+        }
154
+        byte[] bytes = exporter.export(2026, 6, list, "admin");
155
+        try (org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(
156
+                new ByteArrayInputStream(bytes)))
157
+        {
158
+            org.apache.poi.ss.usermodel.Sheet sheet = YakOutboundReportExcelParser.resolveSheet(wb);
159
+            int header = YakOutboundReportExcelParser.detectHeaderRow(sheet);
160
+            int dataStart = header + 1;
161
+            org.apache.poi.ss.usermodel.Row first = sheet.getRow(dataStart);
162
+            org.apache.poi.ss.usermodel.Row third = sheet.getRow(dataStart + 2);
163
+            org.apache.poi.ss.usermodel.Row total = sheet.getRow(dataStart + list.size());
164
+            assertNotNull(first);
165
+            assertNotNull(third);
166
+            assertNotNull(total);
167
+            org.apache.poi.ss.usermodel.BorderStyle expected =
168
+                    first.getCell(0).getCellStyle().getBorderBottom();
169
+            assertNotEquals(org.apache.poi.ss.usermodel.BorderStyle.NONE, expected);
170
+            for (int col = 0; col <= 2; col++)
171
+            {
172
+                assertEquals(expected, third.getCell(col).getCellStyle().getBorderBottom(),
173
+                        "第3数据行列 " + col + " 应有边框");
174
+            }
175
+            for (int col = 0; col <= YakOutboundReportExcelParser.COL_LAST; col++)
176
+            {
177
+                org.apache.poi.ss.usermodel.Cell cell = total.getCell(col);
178
+                assertNotNull(cell, "合计行列 " + col + " 应存在");
179
+                assertNotEquals(org.apache.poi.ss.usermodel.BorderStyle.NONE,
180
+                        cell.getCellStyle().getBorderBottom(),
181
+                        "合计行列 " + col + " 应有下边框");
182
+            }
183
+        }
184
+    }
185
+
144 186
     private static BizYakOutboundReport sample(String town, String village, int households, int yak)
145 187
     {
146 188
         BizYakOutboundReport report = new BizYakOutboundReport();