From 94d5c174aa78399abff1c2acc38005815795f158 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期三, 13 十二月 2023 11:47:35 +0800
Subject: [PATCH] 添加新群号:161281055
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 46 +++++++++++++++++++++++++++++++++++-----------
1 files changed, 35 insertions(+), 11 deletions(-)
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 1d4bfa3..68ccbf1 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
@@ -99,6 +99,11 @@
public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
/**
+ * 用于dictType属性数据存储,避免重复查缓存
+ */
+ public Map<String, String> sysDictMap = new HashMap<String, String>();
+
+ /**
* Excel sheet最大行数,默认65536
*/
public static final int sheetSize = 65536;
@@ -283,9 +288,23 @@
* @param is 输入流
* @return 转换后集合
*/
- public List<T> importExcel(InputStream is) throws Exception
+ public List<T> importExcel(InputStream is)
{
- return importExcel(is, 0);
+ List<T> list = null;
+ try
+ {
+ list = importExcel(is, 0);
+ }
+ catch (Exception e)
+ {
+ log.error("导入Excel异常{}", e.getMessage());
+ throw new UtilException(e.getMessage());
+ }
+ finally
+ {
+ IOUtils.closeQuietly(is);
+ }
+ return list;
}
/**
@@ -331,7 +350,6 @@
}
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
int rows = sheet.getLastRowNum();
-
if (rows > 0)
{
// 定义一个map用于存放excel列的序号和field.
@@ -446,7 +464,7 @@
{
propertyName = field.getName() + "." + attr.targetAttr();
}
- else if (StringUtils.isNotEmpty(attr.readConverterExp()))
+ if (StringUtils.isNotEmpty(attr.readConverterExp()))
{
val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
}
@@ -456,7 +474,7 @@
}
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{
- val = dataFormatHandlerAdapter(val, attr);
+ val = dataFormatHandlerAdapter(val, attr, null);
}
else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
{
@@ -1034,7 +1052,12 @@
}
else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value))
{
- cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator));
+ if (!sysDictMap.containsKey(dictType + value))
+ {
+ String lable = convertDictByExp(Convert.toStr(value), dictType, separator);
+ sysDictMap.put(dictType + value, lable);
+ }
+ cell.setCellValue(sysDictMap.get(dictType + value));
}
else if (value instanceof BigDecimal && -1 != attr.scale())
{
@@ -1042,7 +1065,7 @@
}
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{
- cell.setCellValue(dataFormatHandlerAdapter(value, attr));
+ cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
}
else
{
@@ -1255,13 +1278,13 @@
* @param excel 数据注解
* @return
*/
- public String dataFormatHandlerAdapter(Object value, Excel excel)
+ public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
{
try
{
Object instance = excel.handler().newInstance();
- Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
- value = formatMethod.invoke(instance, value, excel.args());
+ Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class });
+ value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
}
catch (Exception e)
{
@@ -1440,7 +1463,8 @@
Excel[] excels = attrs.value();
for (Excel attr : excels)
{
- if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
+ if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
+ && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
{
field.setAccessible(true);
fields.add(new Object[] { field, attr });
--
Gitblit v1.9.2