From e49253851313e791e2927a5efe32348a88a82101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B2=A9?= Date: Thu, 23 Nov 2023 20:20:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E8=87=AA=E5=AE=9A=E4=B9=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=94=B9=E4=B8=BA=E7=94=B1=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=96=B9=E6=B3=95=E5=88=86=E5=88=AB=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E6=97=B6=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A0=BC=E5=BC=8F=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/utils/poi/ExcelHandlerAdapter.java | 40 +++++++++++++-- .../common/core/utils/poi/ExcelUtil.java | 49 +++++++++++++++++-- 2 files changed, 82 insertions(+), 7 deletions(-) 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 6e0384d3..f342ea3e 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 @@ -434,7 +434,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); } @@ -924,7 +924,7 @@ public class ExcelUtil } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { - cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell)); + cell.setCellValue(exportFormatHandlerAdapter(value, attr, cell)); } else { @@ -1107,10 +1107,13 @@ public class ExcelUtil /** * 数据处理器 * + * @deprecated 方法弃用。分别使用导入导出的两个数据处理器分别处理导入导出的数据 + * * @param value 数据值 * @param excel 数据注解 - * @return + * @return 处理后的结果值 */ + @Deprecated public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell) { try @@ -1126,6 +1129,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); + } + /** * 合计统计信息 */