From 7d94113d24eba8f4e199718f1bbc438cea1b476e Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Wed, 05 Aug 2020 13:02:57 +0800
Subject: [PATCH] 修改Excel设置STRING单元格类型
---
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java | 85 +++++++++++++++++++++++++-----------------
1 files changed, 50 insertions(+), 35 deletions(-)
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 fbfd1dd..823b6d3 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
@@ -6,13 +6,14 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
-import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
@@ -192,7 +193,10 @@
// 设置类的私有字段属性可访问.
field.setAccessible(true);
Integer column = cellMap.get(attr.name());
- fieldsMap.put(column, field);
+ if (column != null)
+ {
+ fieldsMap.put(column, field);
+ }
}
}
for (int i = 1; i < rows; i++)
@@ -263,7 +267,7 @@
}
else if (StringUtils.isNotEmpty(attr.readConverterExp()))
{
- val = reverseByExp(String.valueOf(val), attr.readConverterExp());
+ val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
}
ReflectUtils.invokeSetter(entity, propertyName, val);
}
@@ -463,7 +467,7 @@
{
if (ColumnType.STRING == attr.cellType())
{
- cell.setCellType(CellType.NUMERIC);
+ cell.setCellType(CellType.STRING);
cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
}
else if (ColumnType.NUMERIC == attr.cellType())
@@ -523,13 +527,14 @@
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(Convert.toStr(value), readConverterExp, separator));
}
else
{
@@ -607,28 +612,36 @@
*
* @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)
{
- try
+ StringBuilder propertyString = new StringBuilder();
+ String[] convertSource = converterExp.split(",");
+ for (String item : convertSource)
{
- String[] convertSource = converterExp.split(",");
- for (String item : convertSource)
+ String[] itemArray = item.split("=");
+ if (StringUtils.containsAny(separator, propertyValue))
{
- String[] itemArray = item.split("=");
+ 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];
}
}
}
- catch (Exception e)
- {
- throw e;
- }
- return propertyValue;
+ return StringUtils.stripEnd(propertyString.toString(), separator);
}
/**
@@ -636,28 +649,36 @@
*
* @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)
{
- try
+ StringBuilder propertyString = new StringBuilder();
+ String[] convertSource = converterExp.split(",");
+ for (String item : convertSource)
{
- String[] convertSource = converterExp.split(",");
- for (String item : convertSource)
+ String[] itemArray = item.split("=");
+ if (StringUtils.containsAny(separator, propertyValue))
{
- String[] itemArray = item.split("=");
+ 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];
}
}
}
- catch (Exception e)
- {
- throw e;
- }
- return propertyValue;
+ return StringUtils.stripEnd(propertyString.toString(), separator);
}
/**
@@ -739,6 +760,7 @@
}
}
}
+ this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
}
/**
@@ -809,14 +831,7 @@
}
else
{
- if ((Double) val % 1 > 0)
- {
- val = new DecimalFormat("0.00").format(val);
- }
- else
- {
- val = new DecimalFormat("0").format(val);
- }
+ val = new BigDecimal(val.toString()); // 浮点格式处理
}
}
else if (cell.getCellTypeEnum() == CellType.STRING)
--
Gitblit v1.9.3