|
|
@@ -225,8 +225,6 @@ public class ExcelUtil<T>
|
|
225
|
225
|
{
|
|
226
|
226
|
if (StringUtils.isNotEmpty(title))
|
|
227
|
227
|
{
|
|
228
|
|
- subMergedFirstRowNum++;
|
|
229
|
|
- subMergedLastRowNum++;
|
|
230
|
228
|
int titleLastCol = this.fields.size() - 1;
|
|
231
|
229
|
if (isSubList())
|
|
232
|
230
|
{
|
|
|
@@ -237,7 +235,7 @@ public class ExcelUtil<T>
|
|
237
|
235
|
Cell titleCell = titleRow.createCell(0);
|
|
238
|
236
|
titleCell.setCellStyle(styles.get("title"));
|
|
239
|
237
|
titleCell.setCellValue(title);
|
|
240
|
|
- sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
|
|
|
238
|
+ sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol));
|
|
241
|
239
|
}
|
|
242
|
240
|
}
|
|
243
|
241
|
|
|
|
@@ -248,23 +246,31 @@ public class ExcelUtil<T>
|
|
248
|
246
|
{
|
|
249
|
247
|
if (isSubList())
|
|
250
|
248
|
{
|
|
251
|
|
- subMergedFirstRowNum++;
|
|
252
|
|
- subMergedLastRowNum++;
|
|
253
|
249
|
Row subRow = sheet.createRow(rownum);
|
|
254
|
|
- int excelNum = 0;
|
|
|
250
|
+ int column = 0;
|
|
|
251
|
+ int subFieldSize = subFields != null ? subFields.size() : 0;
|
|
255
|
252
|
for (Object[] objects : fields)
|
|
256
|
253
|
{
|
|
|
254
|
+ Field field = (Field) objects[0];
|
|
257
|
255
|
Excel attr = (Excel) objects[1];
|
|
258
|
|
- Cell headCell1 = subRow.createCell(excelNum);
|
|
259
|
|
- headCell1.setCellValue(attr.name());
|
|
260
|
|
- headCell1.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
|
261
|
|
- excelNum++;
|
|
262
|
|
- }
|
|
263
|
|
- int headFirstRow = excelNum - 1;
|
|
264
|
|
- int headLastRow = headFirstRow + subFields.size() - 1;
|
|
265
|
|
- if (headLastRow > headFirstRow)
|
|
266
|
|
- {
|
|
267
|
|
- sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, headFirstRow, headLastRow));
|
|
|
256
|
+ if (Collection.class.isAssignableFrom(field.getType()))
|
|
|
257
|
+ {
|
|
|
258
|
+ Cell cell = subRow.createCell(column);
|
|
|
259
|
+ cell.setCellValue(attr.name());
|
|
|
260
|
+ cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
|
|
261
|
+ if (subFieldSize > 1)
|
|
|
262
|
+ {
|
|
|
263
|
+ CellRangeAddress cellAddress = new CellRangeAddress(rownum, rownum, column, column + subFieldSize - 1);
|
|
|
264
|
+ sheet.addMergedRegion(cellAddress);
|
|
|
265
|
+ }
|
|
|
266
|
+ column += subFieldSize;
|
|
|
267
|
+ }
|
|
|
268
|
+ else
|
|
|
269
|
+ {
|
|
|
270
|
+ Cell cell = subRow.createCell(column++);
|
|
|
271
|
+ cell.setCellValue(attr.name());
|
|
|
272
|
+ cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
|
|
273
|
+ }
|
|
268
|
274
|
}
|
|
269
|
275
|
rownum++;
|
|
270
|
276
|
}
|
|
|
@@ -589,67 +595,94 @@ public class ExcelUtil<T>
|
|
589
|
595
|
{
|
|
590
|
596
|
int startNo = index * sheetSize;
|
|
591
|
597
|
int endNo = Math.min(startNo + sheetSize, list.size());
|
|
592
|
|
- int rowNo = (1 + rownum) - startNo;
|
|
|
598
|
+ int currentRowNum = rownum + 1; // 从标题行后开始
|
|
|
599
|
+
|
|
593
|
600
|
for (int i = startNo; i < endNo; i++)
|
|
594
|
601
|
{
|
|
595
|
|
- rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo;
|
|
596
|
|
- row = sheet.createRow(rowNo);
|
|
597
|
|
- // 得到导出对象.
|
|
|
602
|
+ row = sheet.createRow(currentRowNum);
|
|
598
|
603
|
T vo = (T) list.get(i);
|
|
599
|
|
- Collection<?> subList = null;
|
|
600
|
|
- if (isSubList())
|
|
601
|
|
- {
|
|
602
|
|
- if (isSubListValue(vo))
|
|
603
|
|
- {
|
|
604
|
|
- subList = getListCellValue(vo);
|
|
605
|
|
- subMergedLastRowNum = subMergedLastRowNum + subList.size();
|
|
606
|
|
- }
|
|
607
|
|
- else
|
|
608
|
|
- {
|
|
609
|
|
- subMergedFirstRowNum++;
|
|
610
|
|
- subMergedLastRowNum++;
|
|
611
|
|
- }
|
|
612
|
|
- }
|
|
613
|
604
|
int column = 0;
|
|
|
605
|
+ int maxSubListSize = getCurrentMaxSubListSize(vo);
|
|
614
|
606
|
for (Object[] os : fields)
|
|
615
|
607
|
{
|
|
616
|
608
|
Field field = (Field) os[0];
|
|
617
|
609
|
Excel excel = (Excel) os[1];
|
|
618
|
|
- if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList))
|
|
|
610
|
+ if (Collection.class.isAssignableFrom(field.getType()))
|
|
619
|
611
|
{
|
|
620
|
|
- boolean subFirst = false;
|
|
621
|
|
- for (Object obj : subList)
|
|
|
612
|
+ try
|
|
622
|
613
|
{
|
|
623
|
|
- if (subFirst)
|
|
|
614
|
+ Collection<?> subList = (Collection<?>) getTargetValue(vo, field, excel);
|
|
|
615
|
+ if (subList != null && !subList.isEmpty())
|
|
624
|
616
|
{
|
|
625
|
|
- rowNo++;
|
|
626
|
|
- row = sheet.createRow(rowNo);
|
|
627
|
|
- }
|
|
628
|
|
- List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(obj.getClass(), Excel.class);
|
|
629
|
|
- int subIndex = 0;
|
|
630
|
|
- for (Field subField : subFields)
|
|
631
|
|
- {
|
|
632
|
|
- if (subField.isAnnotationPresent(Excel.class))
|
|
|
617
|
+ int subIndex = 0;
|
|
|
618
|
+ for (Object subVo : subList)
|
|
633
|
619
|
{
|
|
634
|
|
- subField.setAccessible(true);
|
|
635
|
|
- Excel attr = subField.getAnnotation(Excel.class);
|
|
636
|
|
- this.addCell(attr, row, (T) obj, subField, column + subIndex);
|
|
|
620
|
+ Row subRow = sheet.getRow(currentRowNum + subIndex);
|
|
|
621
|
+ if (subRow == null)
|
|
|
622
|
+ {
|
|
|
623
|
+ subRow = sheet.createRow(currentRowNum + subIndex);
|
|
|
624
|
+ }
|
|
|
625
|
+
|
|
|
626
|
+ int subColumn = column;
|
|
|
627
|
+ for (Field subField : subFields)
|
|
|
628
|
+ {
|
|
|
629
|
+ Excel subExcel = subField.getAnnotation(Excel.class);
|
|
|
630
|
+ addCell(subExcel, subRow, (T) subVo, subField, subColumn++);
|
|
|
631
|
+ }
|
|
|
632
|
+ subIndex++;
|
|
637
|
633
|
}
|
|
638
|
|
- subIndex++;
|
|
|
634
|
+ column += subFields.size();
|
|
639
|
635
|
}
|
|
640
|
|
- subFirst = true;
|
|
641
|
636
|
}
|
|
642
|
|
- this.subMergedFirstRowNum = this.subMergedFirstRowNum + subList.size();
|
|
|
637
|
+ catch (Exception e)
|
|
|
638
|
+ {
|
|
|
639
|
+ log.error("填充集合数据失败", e);
|
|
|
640
|
+ }
|
|
643
|
641
|
}
|
|
644
|
642
|
else
|
|
645
|
643
|
{
|
|
646
|
|
- this.addCell(excel, row, vo, field, column++);
|
|
|
644
|
+ // 创建单元格并设置值
|
|
|
645
|
+ addCell(excel, row, vo, field, column);
|
|
|
646
|
+ if (maxSubListSize > 1 && excel.needMerge())
|
|
|
647
|
+ {
|
|
|
648
|
+ sheet.addMergedRegion(new CellRangeAddress(currentRowNum, currentRowNum + maxSubListSize - 1, column, column));
|
|
|
649
|
+ }
|
|
|
650
|
+ column++;
|
|
647
|
651
|
}
|
|
648
|
652
|
}
|
|
|
653
|
+ currentRowNum += maxSubListSize;
|
|
649
|
654
|
}
|
|
650
|
655
|
}
|
|
651
|
656
|
|
|
652
|
657
|
/**
|
|
|
658
|
+ * 获取子列表最大数
|
|
|
659
|
+ */
|
|
|
660
|
+ private int getCurrentMaxSubListSize(T vo)
|
|
|
661
|
+ {
|
|
|
662
|
+ int maxSubListSize = 1;
|
|
|
663
|
+ for (Object[] os : fields)
|
|
|
664
|
+ {
|
|
|
665
|
+ Field field = (Field) os[0];
|
|
|
666
|
+ if (Collection.class.isAssignableFrom(field.getType()))
|
|
|
667
|
+ {
|
|
|
668
|
+ try
|
|
|
669
|
+ {
|
|
|
670
|
+ Collection<?> subList = (Collection<?>) getTargetValue(vo, field, (Excel) os[1]);
|
|
|
671
|
+ if (subList != null && !subList.isEmpty())
|
|
|
672
|
+ {
|
|
|
673
|
+ maxSubListSize = Math.max(maxSubListSize, subList.size());
|
|
|
674
|
+ }
|
|
|
675
|
+ }
|
|
|
676
|
+ catch (Exception e)
|
|
|
677
|
+ {
|
|
|
678
|
+ log.error("获取集合大小失败", e);
|
|
|
679
|
+ }
|
|
|
680
|
+ }
|
|
|
681
|
+ }
|
|
|
682
|
+ return maxSubListSize;
|
|
|
683
|
+ }
|
|
|
684
|
+
|
|
|
685
|
+ /**
|
|
653
|
686
|
* 创建表格样式
|
|
654
|
687
|
*
|
|
655
|
688
|
* @param wb 工作薄对象
|
|
|
@@ -952,8 +985,10 @@ public class ExcelUtil<T>
|
|
952
|
985
|
cell = row.createCell(column);
|
|
953
|
986
|
if (isSubListValue(vo) && getListCellValue(vo).size() > 1 && attr.needMerge())
|
|
954
|
987
|
{
|
|
955
|
|
- CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
|
|
956
|
|
- sheet.addMergedRegion(cellAddress);
|
|
|
988
|
+ if (subMergedLastRowNum >= subMergedFirstRowNum)
|
|
|
989
|
+ {
|
|
|
990
|
+ sheet.addMergedRegion(new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column));
|
|
|
991
|
+ }
|
|
957
|
992
|
}
|
|
958
|
993
|
cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
|
|
959
|
994
|
|
|
|
@@ -1235,6 +1270,7 @@ public class ExcelUtil<T>
|
|
1235
|
1270
|
*/
|
|
1236
|
1271
|
private Object getTargetValue(T vo, Field field, Excel excel) throws Exception
|
|
1237
|
1272
|
{
|
|
|
1273
|
+ field.setAccessible(true);
|
|
1238
|
1274
|
Object o = field.get(vo);
|
|
1239
|
1275
|
if (StringUtils.isNotEmpty(excel.targetAttr()))
|
|
1240
|
1276
|
{
|
|
|
@@ -1335,7 +1371,6 @@ public class ExcelUtil<T>
|
|
1335
|
1371
|
Excel attr = field.getAnnotation(Excel.class);
|
|
1336
|
1372
|
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
1337
|
1373
|
{
|
|
1338
|
|
- field.setAccessible(true);
|
|
1339
|
1374
|
fields.add(new Object[] { field, attr });
|
|
1340
|
1375
|
}
|
|
1341
|
1376
|
if (Collection.class.isAssignableFrom(field.getType()))
|
|
|
@@ -1359,7 +1394,6 @@ public class ExcelUtil<T>
|
|
1359
|
1394
|
if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr())
|
|
1360
|
1395
|
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
|
|
1361
|
1396
|
{
|
|
1362
|
|
- field.setAccessible(true);
|
|
1363
|
1397
|
fields.add(new Object[] { field, attr });
|
|
1364
|
1398
|
}
|
|
1365
|
1399
|
}
|
|
|
@@ -1368,7 +1402,6 @@ public class ExcelUtil<T>
|
|
1368
|
1402
|
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
|
|
1369
|
1403
|
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
|
|
1370
|
1404
|
{
|
|
1371
|
|
- field.setAccessible(true);
|
|
1372
|
1405
|
fields.add(new Object[] { field, attr });
|
|
1373
|
1406
|
}
|
|
1374
|
1407
|
}
|