Przeglądaj źródła

Excel自定义数据处理器增加单元格/工作簿对象

RuoYi 2 lat temu
rodzic
commit
b2cb085f21

+ 6 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java

@@ -1,5 +1,8 @@
1
 package com.ruoyi.common.core.utils.poi;
1
 package com.ruoyi.common.core.utils.poi;
2
 
2
 
3
+import org.apache.poi.ss.usermodel.Cell;
4
+import org.apache.poi.ss.usermodel.Workbook;
5
+
3
 /**
6
 /**
4
  * Excel数据格式处理适配器
7
  * Excel数据格式处理适配器
5
  * 
8
  * 
@@ -12,8 +15,10 @@ public interface ExcelHandlerAdapter
12
      * 
15
      * 
13
      * @param value 单元格数据值
16
      * @param value 单元格数据值
14
      * @param args excel注解args参数组
17
      * @param args excel注解args参数组
18
+     * @param cell 单元格对象
19
+     * @param wb 工作簿对象
15
      *
20
      *
16
      * @return 处理后的值
21
      * @return 处理后的值
17
      */
22
      */
18
-    Object format(Object value, String[] args);
23
+    Object format(Object value, String[] args, Cell cell, Workbook wb);
19
 }
24
 }

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

@@ -420,7 +420,7 @@ public class ExcelUtil<T>
420
                         }
420
                         }
421
                         else if (!attr.handler().equals(ExcelHandlerAdapter.class))
421
                         else if (!attr.handler().equals(ExcelHandlerAdapter.class))
422
                         {
422
                         {
423
-                            val = dataFormatHandlerAdapter(val, attr);
423
+                            val = dataFormatHandlerAdapter(val, attr, null);
424
                         }
424
                         }
425
                         ReflectUtils.invokeSetter(entity, propertyName, val);
425
                         ReflectUtils.invokeSetter(entity, propertyName, val);
426
                     }
426
                     }
@@ -910,7 +910,7 @@ public class ExcelUtil<T>
910
                 }
910
                 }
911
                 else if (!attr.handler().equals(ExcelHandlerAdapter.class))
911
                 else if (!attr.handler().equals(ExcelHandlerAdapter.class))
912
                 {
912
                 {
913
-                    cell.setCellValue(dataFormatHandlerAdapter(value, attr));
913
+                    cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
914
                 }
914
                 }
915
                 else
915
                 else
916
                 {
916
                 {
@@ -1097,13 +1097,13 @@ public class ExcelUtil<T>
1097
      * @param excel 数据注解
1097
      * @param excel 数据注解
1098
      * @return
1098
      * @return
1099
      */
1099
      */
1100
-    public String dataFormatHandlerAdapter(Object value, Excel excel)
1100
+    public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
1101
     {
1101
     {
1102
         try
1102
         try
1103
         {
1103
         {
1104
             Object instance = excel.handler().newInstance();
1104
             Object instance = excel.handler().newInstance();
1105
-            Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
1106
-            value = formatMethod.invoke(instance, value, excel.args());
1105
+            Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class });
1106
+            value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
1107
         }
1107
         }
1108
         catch (Exception e)
1108
         catch (Exception e)
1109
         {
1109
         {