作者 RuoYi

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

1 package com.ruoyi.common.utils.poi; 1 package com.ruoyi.common.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,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 }
@@ -461,7 +461,7 @@ public class ExcelUtil<T> @@ -461,7 +461,7 @@ public class ExcelUtil<T>
461 } 461 }
462 else if (!attr.handler().equals(ExcelHandlerAdapter.class)) 462 else if (!attr.handler().equals(ExcelHandlerAdapter.class))
463 { 463 {
464 - val = dataFormatHandlerAdapter(val, attr); 464 + val = dataFormatHandlerAdapter(val, attr, null);
465 } 465 }
466 else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) 466 else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
467 { 467 {
@@ -1052,7 +1052,7 @@ public class ExcelUtil<T> @@ -1052,7 +1052,7 @@ public class ExcelUtil<T>
1052 } 1052 }
1053 else if (!attr.handler().equals(ExcelHandlerAdapter.class)) 1053 else if (!attr.handler().equals(ExcelHandlerAdapter.class))
1054 { 1054 {
1055 - cell.setCellValue(dataFormatHandlerAdapter(value, attr)); 1055 + cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
1056 } 1056 }
1057 else 1057 else
1058 { 1058 {
@@ -1265,13 +1265,13 @@ public class ExcelUtil<T> @@ -1265,13 +1265,13 @@ public class ExcelUtil<T>
1265 * @param excel 数据注解 1265 * @param excel 数据注解
1266 * @return 1266 * @return
1267 */ 1267 */
1268 - public String dataFormatHandlerAdapter(Object value, Excel excel) 1268 + public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
1269 { 1269 {
1270 try 1270 try
1271 { 1271 {
1272 Object instance = excel.handler().newInstance(); 1272 Object instance = excel.handler().newInstance();
1273 - Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });  
1274 - value = formatMethod.invoke(instance, value, excel.args()); 1273 + Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class });
  1274 + value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
1275 } 1275 }
1276 catch (Exception e) 1276 catch (Exception e)
1277 { 1277 {