作者 RuoYi

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

@@ -5,6 +5,7 @@ import java.lang.annotation.Retention; @@ -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.utils.poi.ExcelHandlerAdapter;
8 9
9 /** 10 /**
10 * 自定义导出Excel数据注解 11 * 自定义导出Excel数据注解
@@ -108,7 +109,17 @@ public @interface Excel @@ -108,7 +109,17 @@ public @interface Excel
108 /** 109 /**
109 * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) 110 * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
110 */ 111 */
111 - Align align() default Align.AUTO; 112 + public Align align() default Align.AUTO;
  113 +
  114 + /**
  115 + * 自定义数据处理器
  116 + */
  117 + public Class<?> handler() default ExcelHandlerAdapter.class;
  118 +
  119 + /**
  120 + * 自定义数据处理器参数
  121 + */
  122 + public String[] args() default {};
112 123
113 public enum Align 124 public enum Align
114 { 125 {
  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 +}
@@ -6,6 +6,7 @@ import java.io.IOException; @@ -6,6 +6,7 @@ import java.io.IOException;
6 import java.io.InputStream; 6 import java.io.InputStream;
7 import java.io.OutputStream; 7 import java.io.OutputStream;
8 import java.lang.reflect.Field; 8 import java.lang.reflect.Field;
  9 +import java.lang.reflect.Method;
9 import java.math.BigDecimal; 10 import java.math.BigDecimal;
10 import java.text.DecimalFormat; 11 import java.text.DecimalFormat;
11 import java.util.ArrayList; 12 import java.util.ArrayList;
@@ -333,6 +334,10 @@ public class ExcelUtil<T> @@ -333,6 +334,10 @@ public class ExcelUtil<T>
333 { 334 {
334 val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator()); 335 val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
335 } 336 }
  337 + else if (!attr.handler().equals(ExcelHandlerAdapter.class))
  338 + {
  339 + val = dataFormatHandlerAdapter(val, attr);
  340 + }
336 else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) 341 else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
337 { 342 {
338 PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey()); 343 PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
@@ -729,6 +734,10 @@ public class ExcelUtil<T> @@ -729,6 +734,10 @@ public class ExcelUtil<T>
729 { 734 {
730 cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString()); 735 cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
731 } 736 }
  737 + else if (!attr.handler().equals(ExcelHandlerAdapter.class))
  738 + {
  739 + cell.setCellValue(dataFormatHandlerAdapter(value, attr));
  740 + }
732 else 741 else
733 { 742 {
734 // 设置列类型 743 // 设置列类型
@@ -902,6 +911,28 @@ public class ExcelUtil<T> @@ -902,6 +911,28 @@ public class ExcelUtil<T>
902 } 911 }
903 912
904 /** 913 /**
  914 + * 数据处理器
  915 + *
  916 + * @param value 数据值
  917 + * @param excel 数据注解
  918 + * @return
  919 + */
  920 + public String dataFormatHandlerAdapter(Object value, Excel excel)
  921 + {
  922 + try
  923 + {
  924 + Object instance = excel.handler().newInstance();
  925 + Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
  926 + value = formatMethod.invoke(instance, value, excel.args());
  927 + }
  928 + catch (Exception e)
  929 + {
  930 + log.error("不能格式化数据 " + excel.handler(), e.getMessage());
  931 + }
  932 + return Convert.toStr(value);
  933 + }
  934 +
  935 + /**
905 * 合计统计信息 936 * 合计统计信息
906 */ 937 */
907 private void addStatisticsData(Integer index, String text, Excel entity) 938 private void addStatisticsData(Integer index, String text, Excel entity)