diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java index f8f863e0..5aad921e 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java @@ -8,17 +8,49 @@ import org.apache.poi.ss.usermodel.Workbook; * * @author ruoyi */ -public interface ExcelHandlerAdapter -{ +@SuppressWarnings("unused") +public interface ExcelHandlerAdapter { + /** * 格式化 - * + * + * @deprecated 弃用,使用两个方法分别处理导入导出数据格式化 + * * @param value 单元格数据值 * @param args excel注解args参数组 * @param cell 单元格对象 * @param wb 工作簿对象 + * @return 处理后的值 + */ + @Deprecated + default Object format(Object value, String[] args, Cell cell, Workbook wb) { + return value; + } + + /** + * 导入时自定义数据处理器 * + * @param value 单元格数据 + * @param args excel注解args参数组 + * @param cell 单元格对象 + * @param wb 工作簿对象 * @return 处理后的值 */ - Object format(Object value, String[] args, Cell cell, Workbook wb); + default Object importFormat(Object value, String[] args, Cell cell, Workbook wb) { + return value; + } + + /** + * 导出时自定义数据处理器 + * + * @param value 单元格数据 + * @param args excel注解args参数组 + * @param cell 单元格对象 + * @param wb 工作簿对象 + * @return 处理后的值 + */ + default Object exportFormat(Object value, String[] args, Cell cell, Workbook wb) { + return value; + } + } diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java index 3f6ccfb6..8efdc646 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java @@ -455,7 +455,7 @@ public class ExcelUtil } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { - val = dataFormatHandlerAdapter(val, attr, null); + val = importFormatHandlerAdapter(val, attr, null); } ReflectUtils.invokeSetter(entity, propertyName, val); } @@ -1013,7 +1013,7 @@ public class ExcelUtil } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { - cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell)); + cell.setCellValue(exportFormatHandlerAdapter(value, attr, cell)); } else { @@ -1196,10 +1196,13 @@ public class ExcelUtil /** * 数据处理器 * + * @deprecated 方法弃用。分别使用导入导出的两个数据处理器分别处理导入导出的数据 + * * @param value 数据值 * @param excel 数据注解 - * @return + * @return 处理后的结果值 */ + @Deprecated public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell) { try @@ -1215,6 +1218,46 @@ public class ExcelUtil return Convert.toStr(value); } + /** + * 导入时的数据处理器 + * + * @param value 数据值 + * @param excel 数据注解 + * @param cell 单元格对象 + * @return 处理后的结果值 + */ + public String importFormatHandlerAdapter(Object value, Excel excel, Cell cell) { + try { + Object instance = excel.handler().getDeclaredConstructor().newInstance(); + Method formatMethod = excel.handler().getMethod("importFormat", + Object.class, String[].class, Cell.class, Workbook.class); + value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); + } catch (Exception e) { + log.error("不能格式化数据 " + excel.handler() + "{}", e.getMessage()); + } + return Convert.toStr(value); + } + + /** + * 导出时的数据处理器 + * + * @param value 数据值 + * @param excel 数据注解 + * @param cell 单元格对象 + * @return 处理后的结果值 + */ + public String exportFormatHandlerAdapter(Object value, Excel excel, Cell cell) { + try { + Object instance = excel.handler().getDeclaredConstructor().newInstance(); + Method formatMethod = excel.handler().getMethod("exportFormat", + Object.class, String[].class, Cell.class, Workbook.class); + value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); + } catch (Exception e) { + log.error("不能格式化数据 " + excel.handler() + "{}", e.getMessage()); + } + return Convert.toStr(value); + } + /** * 合计统计信息 */