From 326fe6a63f29c8e8a4ddc80411775c29bafbc984 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 21 Jun 2020 11:19:15 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E6=94=AF=E6=8C=81readConverterExp?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=BB=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/annotation/Excel.java | 5 ++ .../com/ruoyi/common/utils/poi/ExcelUtil.java | 53 +++++++++++++++---- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java index 2112128a8..953c68462 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java @@ -29,6 +29,11 @@ public @interface Excel */ public String readConverterExp() default ""; + /** + * 分隔符,读取字符串组内容 + */ + public String separator() default ","; + /** * 导出类型(0数字 1字符串) */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 471adfd0e..461a39ed6 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -276,7 +276,7 @@ public class ExcelUtil } else if (StringUtils.isNotEmpty(attr.readConverterExp())) { - val = reverseByExp(String.valueOf(val), attr.readConverterExp()); + val = reverseByExp(String.valueOf(val), attr.readConverterExp(), attr.separator()); } ReflectUtils.invokeSetter(entity, propertyName, val); } @@ -535,13 +535,14 @@ public class ExcelUtil Object value = getTargetValue(vo, field, attr); String dateFormat = attr.dateFormat(); String readConverterExp = attr.readConverterExp(); + String separator = attr.separator(); if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) { cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value)); } else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) { - cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp)); + cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp, separator)); } else { @@ -619,20 +620,36 @@ public class ExcelUtil * * @param propertyValue 参数值 * @param converterExp 翻译注解 + * @param separator 分隔符 * @return 解析后值 * @throws Exception */ - public static String convertByExp(String propertyValue, String converterExp) throws Exception + public static String convertByExp(String propertyValue, String converterExp, String separator) throws Exception { + StringBuilder propertyString = new StringBuilder(); try { String[] convertSource = converterExp.split(","); for (String item : convertSource) { String[] itemArray = item.split("="); - if (itemArray[0].equals(propertyValue)) + if (StringUtils.containsAny(separator, propertyValue)) { - return itemArray[1]; + for (String value : propertyValue.split(separator)) + { + if (itemArray[0].equals(value)) + { + propertyString.append(itemArray[1] + separator); + break; + } + } + } + else + { + if (itemArray[0].equals(propertyValue)) + { + return itemArray[1]; + } } } } @@ -640,7 +657,7 @@ public class ExcelUtil { throw e; } - return propertyValue; + return StringUtils.stripEnd(propertyString.toString(), separator); } /** @@ -648,20 +665,36 @@ public class ExcelUtil * * @param propertyValue 参数值 * @param converterExp 翻译注解 + * @param separator 分隔符 * @return 解析后值 * @throws Exception */ - public static String reverseByExp(String propertyValue, String converterExp) throws Exception + public static String reverseByExp(String propertyValue, String converterExp, String separator) throws Exception { + StringBuilder propertyString = new StringBuilder(); try { String[] convertSource = converterExp.split(","); for (String item : convertSource) { String[] itemArray = item.split("="); - if (itemArray[1].equals(propertyValue)) + if (StringUtils.containsAny(separator, propertyValue)) { - return itemArray[0]; + for (String value : propertyValue.split(separator)) + { + if (itemArray[1].equals(value)) + { + propertyString.append(itemArray[0] + separator); + break; + } + } + } + else + { + if (itemArray[1].equals(propertyValue)) + { + return itemArray[0]; + } } } } @@ -669,7 +702,7 @@ public class ExcelUtil { throw e; } - return propertyValue; + return StringUtils.stripEnd(propertyString.toString(), separator); } /**