Просмотр исходного кода

Excel注解支持自定义数据处理器

RuoYi лет назад: 4
Родитель
Сommit
0a104689ed

+ 12 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -5,6 +5,7 @@ import java.lang.annotation.Retention;
5
 import java.lang.annotation.RetentionPolicy;
5
 import java.lang.annotation.RetentionPolicy;
6
 import java.lang.annotation.Target;
6
 import java.lang.annotation.Target;
7
 import java.math.BigDecimal;
7
 import java.math.BigDecimal;
8
+import com.ruoyi.common.core.utils.poi.ExcelHandlerAdapter;
8
 
9
 
9
 /**
10
 /**
10
  * 自定义导出Excel数据注解
11
  * 自定义导出Excel数据注解
@@ -103,7 +104,17 @@ public @interface Excel
103
     /**
104
     /**
104
      * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
105
      * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
105
      */
106
      */
106
-    Align align() default Align.AUTO;
107
+    public Align align() default Align.AUTO;
108
+
109
+    /**
110
+     * 自定义数据处理器
111
+     */
112
+    public Class<?> handler() default ExcelHandlerAdapter.class;
113
+
114
+    /**
115
+     * 自定义数据处理器参数
116
+     */
117
+    public String[] args() default {};
107
 
118
 
108
     public enum Align
119
     public enum Align
109
     {
120
     {

+ 19 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java

@@ -0,0 +1,19 @@
1
+package com.ruoyi.common.utils.poi;
2
+
3
+/**
4
+ * Excel数据格式处理适配器
5
+ * 
6
+ * @author ruoyi
7
+ */
8
+public interface ExcelHandlerAdapter
9
+{
10
+    /**
11
+     * 格式化
12
+     * 
13
+     * @param value 单元格数据值
14
+     * @param args excel注解args参数组
15
+     *
16
+     * @return 处理后的值
17
+     */
18
+    Object format(Object value, String[] args);
19
+}

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

@@ -4,6 +4,7 @@ import java.io.IOException;
4
 import java.io.InputStream;
4
 import java.io.InputStream;
5
 import java.io.OutputStream;
5
 import java.io.OutputStream;
6
 import java.lang.reflect.Field;
6
 import java.lang.reflect.Field;
7
+import java.lang.reflect.Method;
7
 import java.math.BigDecimal;
8
 import java.math.BigDecimal;
8
 import java.text.DecimalFormat;
9
 import java.text.DecimalFormat;
9
 import java.util.ArrayList;
10
 import java.util.ArrayList;
@@ -308,6 +309,10 @@ public class ExcelUtil<T>
308
                         {
309
                         {
309
                             val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
310
                             val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
310
                         }
311
                         }
312
+                        else if (!attr.handler().equals(ExcelHandlerAdapter.class))
313
+                        {
314
+                            val = dataFormatHandlerAdapter(val, attr);
315
+                        }
311
                         ReflectUtils.invokeSetter(entity, propertyName, val);
316
                         ReflectUtils.invokeSetter(entity, propertyName, val);
312
                     }
317
                     }
313
                 }
318
                 }
@@ -633,6 +638,10 @@ public class ExcelUtil<T>
633
                 {
638
                 {
634
                     cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
639
                     cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
635
                 }
640
                 }
641
+                else if (!attr.handler().equals(ExcelHandlerAdapter.class))
642
+                {
643
+                    cell.setCellValue(dataFormatHandlerAdapter(value, attr));
644
+                }
636
                 else
645
                 else
637
                 {
646
                 {
638
                     // 设置列类型
647
                     // 设置列类型
@@ -780,6 +789,28 @@ public class ExcelUtil<T>
780
     }
789
     }
781
 
790
 
782
     /**
791
     /**
792
+     * 数据处理器
793
+     * 
794
+     * @param value 数据值
795
+     * @param excel 数据注解
796
+     * @return
797
+     */
798
+    public String dataFormatHandlerAdapter(Object value, Excel excel)
799
+    {
800
+        try
801
+        {
802
+            Object instance = excel.handler().newInstance();
803
+            Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
804
+            value = formatMethod.invoke(instance, value, excel.args());
805
+        }
806
+        catch (Exception e)
807
+        {
808
+            log.error("不能格式化数据 " + excel.handler(), e.getMessage());
809
+        }
810
+        return Convert.toStr(value);
811
+    }
812
+
813
+    /**
783
      * 合计统计信息
814
      * 合计统计信息
784
      */
815
      */
785
     private void addStatisticsData(Integer index, String text, Excel entity)
816
     private void addStatisticsData(Integer index, String text, Excel entity)