Kaynağa Gözat

Excel注解支持自动统计数据总和

RuoYi 5 yıl önce
ebeveyn
işleme
ba3549e824

+ 5 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -96,6 +96,11 @@ public @interface Excel
96 96
     public String targetAttr() default "";
97 97
 
98 98
     /**
99
+     * 是否自动统计数据,在最后追加一行统计数据总和
100
+     */
101
+    public boolean isStatistics() default false;
102
+
103
+    /**
99 104
      * 字段类型(0:导出导入;1:仅导出;2:仅导入)
100 105
      */
101 106
     Type type() default Type.ALL;

+ 69 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -14,6 +14,7 @@ import java.util.Date;
14 14
 import java.util.HashMap;
15 15
 import java.util.List;
16 16
 import java.util.Map;
17
+import java.util.Set;
17 18
 import java.util.stream.Collectors;
18 19
 import javax.servlet.http.HttpServletResponse;
19 20
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
@@ -98,6 +99,16 @@ public class ExcelUtil<T>
98 99
     private List<Object[]> fields;
99 100
 
100 101
     /**
102
+     * 统计列表
103
+     */
104
+    private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
105
+
106
+    /**
107
+     * 数字格式
108
+     */
109
+    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
110
+
111
+    /**
101 112
      * 实体对象
102 113
      */
103 114
     public Class<T> clazz;
@@ -337,6 +348,7 @@ public class ExcelUtil<T>
337 348
                 if (Type.EXPORT.equals(type))
338 349
                 {
339 350
                     fillExcelData(index, row);
351
+                    addStatisticsRow();
340 352
                 }
341 353
             }
342 354
             wb.write(outputStream);
@@ -439,6 +451,15 @@ public class ExcelUtil<T>
439 451
         headerFont.setColor(IndexedColors.WHITE.getIndex());
440 452
         style.setFont(headerFont);
441 453
         styles.put("header", style);
454
+        
455
+        style = wb.createCellStyle();
456
+        style.setAlignment(HorizontalAlignment.CENTER);
457
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
458
+        Font totalFont = wb.createFont();
459
+        totalFont.setFontName("Arial");
460
+        totalFont.setFontHeightInPoints((short) 10);
461
+        style.setFont(totalFont);
462
+        styles.put("total", style);
442 463
 
443 464
         return styles;
444 465
     }
@@ -546,6 +567,7 @@ public class ExcelUtil<T>
546 567
                     // 设置列类型
547 568
                     setCellVo(value, attr, cell);
548 569
                 }
570
+                addStatisticsData(column, Convert.toStr(value), attr);
549 571
             }
550 572
         }
551 573
         catch (Exception e)
@@ -687,6 +709,53 @@ public class ExcelUtil<T>
687 709
     }
688 710
 
689 711
     /**
712
+     * 合计统计信息
713
+     */
714
+    private void addStatisticsData(Integer index, String text, Excel entity)
715
+    {
716
+        if (entity != null && entity.isStatistics())
717
+        {
718
+            Double temp = 0D;
719
+            if (!statistics.containsKey(index))
720
+            {
721
+                statistics.put(index, temp);
722
+            }
723
+            try
724
+            {
725
+                temp = Double.valueOf(text);
726
+            }
727
+            catch (NumberFormatException e)
728
+            {
729
+            }
730
+            statistics.put(index, statistics.get(index) + temp);
731
+        }
732
+    }
733
+
734
+    /**
735
+     * 创建统计行
736
+     */
737
+    public void addStatisticsRow()
738
+    {
739
+        if (statistics.size() > 0)
740
+        {
741
+            Cell cell = null;
742
+            Row row = sheet.createRow(sheet.getLastRowNum() + 1);
743
+            Set<Integer> keys = statistics.keySet();
744
+            cell = row.createCell(0);
745
+            cell.setCellStyle(styles.get("total"));
746
+            cell.setCellValue("合计");
747
+
748
+            for (Integer key : keys)
749
+            {
750
+                cell = row.createCell(key);
751
+                cell.setCellStyle(styles.get("total"));
752
+                cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key)));
753
+            }
754
+            statistics.clear();
755
+        }
756
+    }
757
+
758
+    /**
690 759
      * 获取bean中的属性值
691 760
      * 
692 761
      * @param vo 实体对象