|
|
@@ -30,8 +30,6 @@ public class YakHerdInventoryExcelExporter
|
|
30
|
30
|
|
|
31
|
31
|
private static final int COL_SEQ = 0;
|
|
32
|
32
|
|
|
33
|
|
- private static final int COL_F_YAK_MIRROR = 5;
|
|
34
|
|
-
|
|
35
|
33
|
private static final Pattern YEAR_IN_TEXT = Pattern.compile("(\\d{4})年");
|
|
36
|
34
|
|
|
37
|
35
|
public byte[] export(Integer statYear, List<SysDept> towns, Map<Long, BizYakHerdInventory> inventoryByTownId)
|
|
|
@@ -45,8 +43,24 @@ public class YakHerdInventoryExcelExporter
|
|
45
|
43
|
throw new ServiceException("导出年份不在可选范围内");
|
|
46
|
44
|
}
|
|
47
|
45
|
try (InputStream in = new ClassPathResource(TEMPLATE_CLASSPATH).getInputStream();
|
|
48
|
|
- Workbook workbook = WorkbookFactory.create(in);
|
|
49
|
|
- ByteArrayOutputStream out = new ByteArrayOutputStream())
|
|
|
46
|
+ Workbook workbook = WorkbookFactory.create(in))
|
|
|
47
|
+ {
|
|
|
48
|
+ return exportWorkbook(workbook, statYear, towns, inventoryByTownId);
|
|
|
49
|
+ }
|
|
|
50
|
+ catch (ServiceException ex)
|
|
|
51
|
+ {
|
|
|
52
|
+ throw ex;
|
|
|
53
|
+ }
|
|
|
54
|
+ catch (Exception ex)
|
|
|
55
|
+ {
|
|
|
56
|
+ throw new ServiceException("导出 Excel 失败:" + ex.getMessage());
|
|
|
57
|
+ }
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ byte[] exportWorkbook(Workbook workbook, Integer statYear, List<SysDept> towns,
|
|
|
61
|
+ Map<Long, BizYakHerdInventory> inventoryByTownId)
|
|
|
62
|
+ {
|
|
|
63
|
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream())
|
|
50
|
64
|
{
|
|
51
|
65
|
Sheet sheet = workbook.getSheetAt(0);
|
|
52
|
66
|
int headerRowIndex = YakHerdInventoryExcelParser.detectHeaderRow(sheet);
|
|
|
@@ -56,6 +70,7 @@ public class YakHerdInventoryExcelExporter
|
|
56
|
70
|
}
|
|
57
|
71
|
updateTitleYear(sheet, statYear);
|
|
58
|
72
|
int dataStartRow = headerRowIndex + 3;
|
|
|
73
|
+ clearTemplateDataRegion(sheet, dataStartRow);
|
|
59
|
74
|
Totals totals = new Totals();
|
|
60
|
75
|
int rowIndex = dataStartRow;
|
|
61
|
76
|
int seq = 1;
|
|
|
@@ -77,16 +92,7 @@ public class YakHerdInventoryExcelExporter
|
|
77
|
92
|
clearDataCells(totalRow);
|
|
78
|
93
|
setStringCell(totalRow, YakHerdInventoryExcelParser.COL_TOWN, "合计");
|
|
79
|
94
|
totals.writeToRow(totalRow);
|
|
80
|
|
- // 清除模板中可能残留的旧数据行
|
|
81
|
|
- for (int i = rowIndex + 1; i <= sheet.getLastRowNum(); i++)
|
|
82
|
|
- {
|
|
83
|
|
- Row stale = sheet.getRow(i);
|
|
84
|
|
- if (stale != null)
|
|
85
|
|
- {
|
|
86
|
|
- clearDataCells(stale);
|
|
87
|
|
- setStringCell(stale, YakHerdInventoryExcelParser.COL_TOWN, null);
|
|
88
|
|
- }
|
|
89
|
|
- }
|
|
|
95
|
+ clearTemplateDataRegion(sheet, rowIndex + 1);
|
|
90
|
96
|
workbook.write(out);
|
|
91
|
97
|
return out.toByteArray();
|
|
92
|
98
|
}
|
|
|
@@ -100,6 +106,21 @@ public class YakHerdInventoryExcelExporter
|
|
100
|
106
|
}
|
|
101
|
107
|
}
|
|
102
|
108
|
|
|
|
109
|
+ private static void clearTemplateDataRegion(Sheet sheet, int fromRow)
|
|
|
110
|
+ {
|
|
|
111
|
+ int lastRow = sheet.getLastRowNum();
|
|
|
112
|
+ for (int i = fromRow; i <= lastRow; i++)
|
|
|
113
|
+ {
|
|
|
114
|
+ Row row = sheet.getRow(i);
|
|
|
115
|
+ if (row == null)
|
|
|
116
|
+ {
|
|
|
117
|
+ continue;
|
|
|
118
|
+ }
|
|
|
119
|
+ clearDataCells(row);
|
|
|
120
|
+ setStringCell(row, YakHerdInventoryExcelParser.COL_TOWN, null);
|
|
|
121
|
+ }
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
103
|
124
|
static void updateTitleYear(Sheet sheet, int statYear)
|
|
104
|
125
|
{
|
|
105
|
126
|
DataFormatter formatter = new DataFormatter();
|
|
|
@@ -139,30 +160,20 @@ public class YakHerdInventoryExcelExporter
|
|
139
|
160
|
|
|
140
|
161
|
private static void clearDataCells(Row row)
|
|
141
|
162
|
{
|
|
142
|
|
- int[] cols = new int[] {
|
|
143
|
|
- COL_SEQ,
|
|
144
|
|
- YakHerdInventoryExcelParser.COL_TOWN,
|
|
145
|
|
- YakHerdInventoryExcelParser.COL_VILLAGE_COUNT,
|
|
146
|
|
- YakHerdInventoryExcelParser.COL_HERDSMAN,
|
|
147
|
|
- YakHerdInventoryExcelParser.COL_YAK_TOTAL,
|
|
148
|
|
- COL_F_YAK_MIRROR,
|
|
149
|
|
- YakHerdInventoryExcelParser.COL_BULL_TOTAL,
|
|
150
|
|
- YakHerdInventoryExcelParser.COL_BREEDING_BULL,
|
|
151
|
|
- YakHerdInventoryExcelParser.COL_BULL_CASTRATED,
|
|
152
|
|
- YakHerdInventoryExcelParser.COL_COW_TOTAL,
|
|
153
|
|
- YakHerdInventoryExcelParser.COL_COW_UNDER_1,
|
|
154
|
|
- YakHerdInventoryExcelParser.COL_COW_1_3,
|
|
155
|
|
- YakHerdInventoryExcelParser.COL_COW_3_12,
|
|
156
|
|
- YakHerdInventoryExcelParser.COL_COW_OVER_12,
|
|
157
|
|
- YakHerdInventoryExcelParser.COL_REMARK
|
|
158
|
|
- };
|
|
159
|
|
- for (int col : cols)
|
|
|
163
|
+ short lastCell = row.getLastCellNum();
|
|
|
164
|
+ if (lastCell < 0)
|
|
|
165
|
+ {
|
|
|
166
|
+ return;
|
|
|
167
|
+ }
|
|
|
168
|
+ int maxCol = Math.max(lastCell, YakHerdInventoryExcelParser.COL_REMARK + 1);
|
|
|
169
|
+ for (int col = 0; col < maxCol; col++)
|
|
160
|
170
|
{
|
|
161
|
171
|
Cell cell = row.getCell(col);
|
|
162
|
|
- if (cell != null)
|
|
|
172
|
+ if (cell == null)
|
|
163
|
173
|
{
|
|
164
|
|
- row.removeCell(cell);
|
|
|
174
|
+ continue;
|
|
165
|
175
|
}
|
|
|
176
|
+ cell.setBlank();
|
|
166
|
177
|
}
|
|
167
|
178
|
}
|
|
168
|
179
|
|
|
|
@@ -181,8 +192,10 @@ public class YakHerdInventoryExcelExporter
|
|
181
|
192
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_VILLAGE_COUNT, inv.getVillageCount());
|
|
182
|
193
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_HERDSMAN, inv.getHerdsmanHouseholdCount());
|
|
183
|
194
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_YAK_TOTAL, inv.getYakTotal());
|
|
184
|
|
- setIntegerCell(row, COL_F_YAK_MIRROR, inv.getYakTotal());
|
|
|
195
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_YAK_GENDER_TOTAL, inv.getYakTotal());
|
|
185
|
196
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_TOTAL, inv.getBullTotal());
|
|
|
197
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_COW_HEAD, inv.getCowTotal());
|
|
|
198
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_SECTION_TOTAL, inv.getBullTotal());
|
|
186
|
199
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BREEDING_BULL, inv.getBreedingBullCount());
|
|
187
|
200
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_CASTRATED, inv.getBullCastratedCount());
|
|
188
|
201
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_COW_TOTAL, inv.getCowTotal());
|
|
|
@@ -283,8 +296,10 @@ public class YakHerdInventoryExcelExporter
|
|
283
|
296
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_VILLAGE_COUNT, villageCount);
|
|
284
|
297
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_HERDSMAN, herdsmanHouseholdCount);
|
|
285
|
298
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_YAK_TOTAL, yakTotal);
|
|
286
|
|
- setIntegerCell(row, COL_F_YAK_MIRROR, yakTotal);
|
|
|
299
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_YAK_GENDER_TOTAL, yakTotal);
|
|
287
|
300
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_TOTAL, bullTotal);
|
|
|
301
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_COW_HEAD, cowTotal);
|
|
|
302
|
+ setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_SECTION_TOTAL, bullTotal);
|
|
288
|
303
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BREEDING_BULL, breedingBullCount);
|
|
289
|
304
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_BULL_CASTRATED, bullCastratedCount);
|
|
290
|
305
|
setIntegerCell(row, YakHerdInventoryExcelParser.COL_COW_TOTAL, cowTotal);
|