正在显示
1 个修改的文件
包含
86 行增加
和
53 行删除
| @@ -252,8 +252,6 @@ public class ExcelUtil<T> | @@ -252,8 +252,6 @@ public class ExcelUtil<T> | ||
| 252 | { | 252 | { |
| 253 | if (StringUtils.isNotEmpty(title)) | 253 | if (StringUtils.isNotEmpty(title)) |
| 254 | { | 254 | { |
| 255 | - subMergedFirstRowNum++; | ||
| 256 | - subMergedLastRowNum++; | ||
| 257 | int titleLastCol = this.fields.size() - 1; | 255 | int titleLastCol = this.fields.size() - 1; |
| 258 | if (isSubList()) | 256 | if (isSubList()) |
| 259 | { | 257 | { |
| @@ -264,7 +262,7 @@ public class ExcelUtil<T> | @@ -264,7 +262,7 @@ public class ExcelUtil<T> | ||
| 264 | Cell titleCell = titleRow.createCell(0); | 262 | Cell titleCell = titleRow.createCell(0); |
| 265 | titleCell.setCellStyle(styles.get("title")); | 263 | titleCell.setCellStyle(styles.get("title")); |
| 266 | titleCell.setCellValue(title); | 264 | titleCell.setCellValue(title); |
| 267 | - sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol)); | 265 | + sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol)); |
| 268 | } | 266 | } |
| 269 | } | 267 | } |
| 270 | 268 | ||
| @@ -275,23 +273,31 @@ public class ExcelUtil<T> | @@ -275,23 +273,31 @@ public class ExcelUtil<T> | ||
| 275 | { | 273 | { |
| 276 | if (isSubList()) | 274 | if (isSubList()) |
| 277 | { | 275 | { |
| 278 | - subMergedFirstRowNum++; | ||
| 279 | - subMergedLastRowNum++; | ||
| 280 | Row subRow = sheet.createRow(rownum); | 276 | Row subRow = sheet.createRow(rownum); |
| 281 | - int excelNum = 0; | 277 | + int column = 0; |
| 278 | + int subFieldSize = subFields != null ? subFields.size() : 0; | ||
| 282 | for (Object[] objects : fields) | 279 | for (Object[] objects : fields) |
| 283 | { | 280 | { |
| 281 | + Field field = (Field) objects[0]; | ||
| 284 | Excel attr = (Excel) objects[1]; | 282 | Excel attr = (Excel) objects[1]; |
| 285 | - Cell headCell1 = subRow.createCell(excelNum); | ||
| 286 | - headCell1.setCellValue(attr.name()); | ||
| 287 | - headCell1.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); | ||
| 288 | - excelNum++; | 283 | + if (Collection.class.isAssignableFrom(field.getType())) |
| 284 | + { | ||
| 285 | + Cell cell = subRow.createCell(column); | ||
| 286 | + cell.setCellValue(attr.name()); | ||
| 287 | + cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); | ||
| 288 | + if (subFieldSize > 1) | ||
| 289 | + { | ||
| 290 | + CellRangeAddress cellAddress = new CellRangeAddress(rownum, rownum, column, column + subFieldSize - 1); | ||
| 291 | + sheet.addMergedRegion(cellAddress); | ||
| 292 | + } | ||
| 293 | + column += subFieldSize; | ||
| 289 | } | 294 | } |
| 290 | - int headFirstRow = excelNum - 1; | ||
| 291 | - int headLastRow = headFirstRow + subFields.size() - 1; | ||
| 292 | - if (headLastRow > headFirstRow) | 295 | + else |
| 293 | { | 296 | { |
| 294 | - sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, headFirstRow, headLastRow)); | 297 | + Cell cell = subRow.createCell(column++); |
| 298 | + cell.setCellValue(attr.name()); | ||
| 299 | + cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); | ||
| 300 | + } | ||
| 295 | } | 301 | } |
| 296 | rownum++; | 302 | rownum++; |
| 297 | } | 303 | } |
| @@ -725,67 +731,94 @@ public class ExcelUtil<T> | @@ -725,67 +731,94 @@ public class ExcelUtil<T> | ||
| 725 | { | 731 | { |
| 726 | int startNo = index * sheetSize; | 732 | int startNo = index * sheetSize; |
| 727 | int endNo = Math.min(startNo + sheetSize, list.size()); | 733 | int endNo = Math.min(startNo + sheetSize, list.size()); |
| 728 | - int rowNo = (1 + rownum) - startNo; | 734 | + int currentRowNum = rownum + 1; // 从标题行后开始 |
| 735 | + | ||
| 729 | for (int i = startNo; i < endNo; i++) | 736 | for (int i = startNo; i < endNo; i++) |
| 730 | { | 737 | { |
| 731 | - rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo; | ||
| 732 | - row = sheet.createRow(rowNo); | ||
| 733 | - // 得到导出对象. | 738 | + row = sheet.createRow(currentRowNum); |
| 734 | T vo = (T) list.get(i); | 739 | T vo = (T) list.get(i); |
| 735 | - Collection<?> subList = null; | ||
| 736 | - if (isSubList()) | ||
| 737 | - { | ||
| 738 | - if (isSubListValue(vo)) | ||
| 739 | - { | ||
| 740 | - subList = getListCellValue(vo); | ||
| 741 | - subMergedLastRowNum = subMergedLastRowNum + subList.size(); | ||
| 742 | - } | ||
| 743 | - else | ||
| 744 | - { | ||
| 745 | - subMergedFirstRowNum++; | ||
| 746 | - subMergedLastRowNum++; | ||
| 747 | - } | ||
| 748 | - } | ||
| 749 | int column = 0; | 740 | int column = 0; |
| 741 | + int maxSubListSize = getCurrentMaxSubListSize(vo); | ||
| 750 | for (Object[] os : fields) | 742 | for (Object[] os : fields) |
| 751 | { | 743 | { |
| 752 | Field field = (Field) os[0]; | 744 | Field field = (Field) os[0]; |
| 753 | Excel excel = (Excel) os[1]; | 745 | Excel excel = (Excel) os[1]; |
| 754 | - if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList)) | 746 | + if (Collection.class.isAssignableFrom(field.getType())) |
| 755 | { | 747 | { |
| 756 | - boolean subFirst = false; | ||
| 757 | - for (Object obj : subList) | 748 | + try |
| 758 | { | 749 | { |
| 759 | - if (subFirst) | 750 | + Collection<?> subList = (Collection<?>) getTargetValue(vo, field, excel); |
| 751 | + if (subList != null && !subList.isEmpty()) | ||
| 760 | { | 752 | { |
| 761 | - rowNo++; | ||
| 762 | - row = sheet.createRow(rowNo); | ||
| 763 | - } | ||
| 764 | - List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(obj.getClass(), Excel.class); | ||
| 765 | int subIndex = 0; | 753 | int subIndex = 0; |
| 766 | - for (Field subField : subFields) | 754 | + for (Object subVo : subList) |
| 755 | + { | ||
| 756 | + Row subRow = sheet.getRow(currentRowNum + subIndex); | ||
| 757 | + if (subRow == null) | ||
| 767 | { | 758 | { |
| 768 | - if (subField.isAnnotationPresent(Excel.class)) | 759 | + subRow = sheet.createRow(currentRowNum + subIndex); |
| 760 | + } | ||
| 761 | + | ||
| 762 | + int subColumn = column; | ||
| 763 | + for (Field subField : subFields) | ||
| 769 | { | 764 | { |
| 770 | - subField.setAccessible(true); | ||
| 771 | - Excel attr = subField.getAnnotation(Excel.class); | ||
| 772 | - this.addCell(attr, row, (T) obj, subField, column + subIndex); | 765 | + Excel subExcel = subField.getAnnotation(Excel.class); |
| 766 | + addCell(subExcel, subRow, (T) subVo, subField, subColumn++); | ||
| 773 | } | 767 | } |
| 774 | subIndex++; | 768 | subIndex++; |
| 775 | } | 769 | } |
| 776 | - subFirst = true; | 770 | + column += subFields.size(); |
| 771 | + } | ||
| 772 | + } | ||
| 773 | + catch (Exception e) | ||
| 774 | + { | ||
| 775 | + log.error("填充集合数据失败", e); | ||
| 777 | } | 776 | } |
| 778 | - this.subMergedFirstRowNum = this.subMergedFirstRowNum + subList.size(); | ||
| 779 | } | 777 | } |
| 780 | else | 778 | else |
| 781 | { | 779 | { |
| 782 | - this.addCell(excel, row, vo, field, column++); | 780 | + // 创建单元格并设置值 |
| 781 | + addCell(excel, row, vo, field, column); | ||
| 782 | + if (maxSubListSize > 1 && excel.needMerge()) | ||
| 783 | + { | ||
| 784 | + sheet.addMergedRegion(new CellRangeAddress(currentRowNum, currentRowNum + maxSubListSize - 1, column, column)); | ||
| 785 | + } | ||
| 786 | + column++; | ||
| 783 | } | 787 | } |
| 784 | } | 788 | } |
| 789 | + currentRowNum += maxSubListSize; | ||
| 785 | } | 790 | } |
| 786 | } | 791 | } |
| 787 | 792 | ||
| 788 | /** | 793 | /** |
| 794 | + * 获取子列表最大数 | ||
| 795 | + */ | ||
| 796 | + private int getCurrentMaxSubListSize(T vo) | ||
| 797 | + { | ||
| 798 | + int maxSubListSize = 1; | ||
| 799 | + for (Object[] os : fields) | ||
| 800 | + { | ||
| 801 | + Field field = (Field) os[0]; | ||
| 802 | + if (Collection.class.isAssignableFrom(field.getType())) | ||
| 803 | + { | ||
| 804 | + try | ||
| 805 | + { | ||
| 806 | + Collection<?> subList = (Collection<?>) getTargetValue(vo, field, (Excel) os[1]); | ||
| 807 | + if (subList != null && !subList.isEmpty()) | ||
| 808 | + { | ||
| 809 | + maxSubListSize = Math.max(maxSubListSize, subList.size()); | ||
| 810 | + } | ||
| 811 | + } | ||
| 812 | + catch (Exception e) | ||
| 813 | + { | ||
| 814 | + log.error("获取集合大小失败", e); | ||
| 815 | + } | ||
| 816 | + } | ||
| 817 | + } | ||
| 818 | + return maxSubListSize; | ||
| 819 | + } | ||
| 820 | + | ||
| 821 | + /** | ||
| 789 | * 创建表格样式 | 822 | * 创建表格样式 |
| 790 | * | 823 | * |
| 791 | * @param wb 工作薄对象 | 824 | * @param wb 工作薄对象 |
| @@ -1099,8 +1132,10 @@ public class ExcelUtil<T> | @@ -1099,8 +1132,10 @@ public class ExcelUtil<T> | ||
| 1099 | cell = row.createCell(column); | 1132 | cell = row.createCell(column); |
| 1100 | if (isSubListValue(vo) && getListCellValue(vo).size() > 1 && attr.needMerge()) | 1133 | if (isSubListValue(vo) && getListCellValue(vo).size() > 1 && attr.needMerge()) |
| 1101 | { | 1134 | { |
| 1102 | - CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column); | ||
| 1103 | - sheet.addMergedRegion(cellAddress); | 1135 | + if (subMergedLastRowNum >= subMergedFirstRowNum) |
| 1136 | + { | ||
| 1137 | + sheet.addMergedRegion(new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column)); | ||
| 1138 | + } | ||
| 1104 | } | 1139 | } |
| 1105 | cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType()))); | 1140 | cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType()))); |
| 1106 | 1141 | ||
| @@ -1443,6 +1478,7 @@ public class ExcelUtil<T> | @@ -1443,6 +1478,7 @@ public class ExcelUtil<T> | ||
| 1443 | */ | 1478 | */ |
| 1444 | private Object getTargetValue(T vo, Field field, Excel excel) throws Exception | 1479 | private Object getTargetValue(T vo, Field field, Excel excel) throws Exception |
| 1445 | { | 1480 | { |
| 1481 | + field.setAccessible(true); | ||
| 1446 | Object o = field.get(vo); | 1482 | Object o = field.get(vo); |
| 1447 | if (StringUtils.isNotEmpty(excel.targetAttr())) | 1483 | if (StringUtils.isNotEmpty(excel.targetAttr())) |
| 1448 | { | 1484 | { |
| @@ -1543,7 +1579,6 @@ public class ExcelUtil<T> | @@ -1543,7 +1579,6 @@ public class ExcelUtil<T> | ||
| 1543 | Excel attr = field.getAnnotation(Excel.class); | 1579 | Excel attr = field.getAnnotation(Excel.class); |
| 1544 | if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) | 1580 | if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) |
| 1545 | { | 1581 | { |
| 1546 | - field.setAccessible(true); | ||
| 1547 | fields.add(new Object[] { field, attr }); | 1582 | fields.add(new Object[] { field, attr }); |
| 1548 | } | 1583 | } |
| 1549 | if (Collection.class.isAssignableFrom(field.getType())) | 1584 | if (Collection.class.isAssignableFrom(field.getType())) |
| @@ -1567,7 +1602,6 @@ public class ExcelUtil<T> | @@ -1567,7 +1602,6 @@ public class ExcelUtil<T> | ||
| 1567 | if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr()) | 1602 | if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr()) |
| 1568 | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) | 1603 | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) |
| 1569 | { | 1604 | { |
| 1570 | - field.setAccessible(true); | ||
| 1571 | fields.add(new Object[] { field, attr }); | 1605 | fields.add(new Object[] { field, attr }); |
| 1572 | } | 1606 | } |
| 1573 | } | 1607 | } |
| @@ -1576,7 +1610,6 @@ public class ExcelUtil<T> | @@ -1576,7 +1610,6 @@ public class ExcelUtil<T> | ||
| 1576 | if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) | 1610 | if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) |
| 1577 | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) | 1611 | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) |
| 1578 | { | 1612 | { |
| 1579 | - field.setAccessible(true); | ||
| 1580 | fields.add(new Object[] { field, attr }); | 1613 | fields.add(new Object[] { field, attr }); |
| 1581 | } | 1614 | } |
| 1582 | } | 1615 | } |
-
请 注册 或 登录 后发表评论