Commit bf771ae5c8a0b76015f4d8bc60729231559f9090
1 parent
fa5596bb
Excel注解支持自动统计数据总和
Showing
2 changed files
with
74 additions
and
0 deletions
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
| ... | ... | @@ -101,6 +101,11 @@ public @interface Excel |
| 101 | 101 | public String targetAttr() default ""; |
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | + * 是否自动统计数据,在最后追加一行统计数据总和 | |
| 105 | + */ | |
| 106 | + public boolean isStatistics() default false; | |
| 107 | + | |
| 108 | + /** | |
| 104 | 109 | * 字段类型(0:导出导入;1:仅导出;2:仅导入) |
| 105 | 110 | */ |
| 106 | 111 | Type type() default Type.ALL; | ... | ... |
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
| ... | ... | @@ -16,6 +16,7 @@ import java.util.Date; |
| 16 | 16 | import java.util.HashMap; |
| 17 | 17 | import java.util.List; |
| 18 | 18 | import java.util.Map; |
| 19 | +import java.util.Set; | |
| 19 | 20 | import java.util.UUID; |
| 20 | 21 | import java.util.stream.Collectors; |
| 21 | 22 | import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
| ... | ... | @@ -104,6 +105,16 @@ public class ExcelUtil<T> |
| 104 | 105 | private List<Object[]> fields; |
| 105 | 106 | |
| 106 | 107 | /** |
| 108 | + * 统计列表 | |
| 109 | + */ | |
| 110 | + private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * 数字格式 | |
| 114 | + */ | |
| 115 | + private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | |
| 116 | + | |
| 117 | + /** | |
| 107 | 118 | * 实体对象 |
| 108 | 119 | */ |
| 109 | 120 | public Class<T> clazz; |
| ... | ... | @@ -342,6 +353,7 @@ public class ExcelUtil<T> |
| 342 | 353 | if (Type.EXPORT.equals(type)) |
| 343 | 354 | { |
| 344 | 355 | fillExcelData(index, row); |
| 356 | + addStatisticsRow(); | |
| 345 | 357 | } |
| 346 | 358 | } |
| 347 | 359 | String filename = encodingFilename(sheetName); |
| ... | ... | @@ -448,6 +460,15 @@ public class ExcelUtil<T> |
| 448 | 460 | headerFont.setColor(IndexedColors.WHITE.getIndex()); |
| 449 | 461 | style.setFont(headerFont); |
| 450 | 462 | styles.put("header", style); |
| 463 | + | |
| 464 | + style = wb.createCellStyle(); | |
| 465 | + style.setAlignment(HorizontalAlignment.CENTER); | |
| 466 | + style.setVerticalAlignment(VerticalAlignment.CENTER); | |
| 467 | + Font totalFont = wb.createFont(); | |
| 468 | + totalFont.setFontName("Arial"); | |
| 469 | + totalFont.setFontHeightInPoints((short) 10); | |
| 470 | + style.setFont(totalFont); | |
| 471 | + styles.put("total", style); | |
| 451 | 472 | |
| 452 | 473 | return styles; |
| 453 | 474 | } |
| ... | ... | @@ -560,6 +581,7 @@ public class ExcelUtil<T> |
| 560 | 581 | // 设置列类型 |
| 561 | 582 | setCellVo(value, attr, cell); |
| 562 | 583 | } |
| 584 | + addStatisticsData(column, Convert.toStr(value), attr); | |
| 563 | 585 | } |
| 564 | 586 | } |
| 565 | 587 | catch (Exception e) |
| ... | ... | @@ -727,6 +749,53 @@ public class ExcelUtil<T> |
| 727 | 749 | } |
| 728 | 750 | |
| 729 | 751 | /** |
| 752 | + * 合计统计信息 | |
| 753 | + */ | |
| 754 | + private void addStatisticsData(Integer index, String text, Excel entity) | |
| 755 | + { | |
| 756 | + if (entity != null && entity.isStatistics()) | |
| 757 | + { | |
| 758 | + Double temp = 0D; | |
| 759 | + if (!statistics.containsKey(index)) | |
| 760 | + { | |
| 761 | + statistics.put(index, temp); | |
| 762 | + } | |
| 763 | + try | |
| 764 | + { | |
| 765 | + temp = Double.valueOf(text); | |
| 766 | + } | |
| 767 | + catch (NumberFormatException e) | |
| 768 | + { | |
| 769 | + } | |
| 770 | + statistics.put(index, statistics.get(index) + temp); | |
| 771 | + } | |
| 772 | + } | |
| 773 | + | |
| 774 | + /** | |
| 775 | + * 创建统计行 | |
| 776 | + */ | |
| 777 | + public void addStatisticsRow() | |
| 778 | + { | |
| 779 | + if (statistics.size() > 0) | |
| 780 | + { | |
| 781 | + Cell cell = null; | |
| 782 | + Row row = sheet.createRow(sheet.getLastRowNum() + 1); | |
| 783 | + Set<Integer> keys = statistics.keySet(); | |
| 784 | + cell = row.createCell(0); | |
| 785 | + cell.setCellStyle(styles.get("total")); | |
| 786 | + cell.setCellValue("合计"); | |
| 787 | + | |
| 788 | + for (Integer key : keys) | |
| 789 | + { | |
| 790 | + cell = row.createCell(key); | |
| 791 | + cell.setCellStyle(styles.get("total")); | |
| 792 | + cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key))); | |
| 793 | + } | |
| 794 | + statistics.clear(); | |
| 795 | + } | |
| 796 | + } | |
| 797 | + | |
| 798 | + /** | |
| 730 | 799 | * 编码文件名 |
| 731 | 800 | */ |
| 732 | 801 | public String encodingFilename(String filename) | ... | ... |