From 6a742e1d1bb1fc8c7809ab993367c85daf7a54b4 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Sat, 19 Aug 2023 15:43:57 +0800
Subject: [PATCH] Excel导入数据临时文件无法删除问题(I7KIXX)
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 29 +++++++++++++++++++++--------
1 files changed, 21 insertions(+), 8 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 cbe47b6..349ee75 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
@@ -288,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;
}
/**
@@ -336,7 +350,6 @@
}
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
int rows = sheet.getLastRowNum();
-
if (rows > 0)
{
// 定义一个map用于存放excel列的序号和field.
@@ -461,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))
{
@@ -1052,7 +1065,7 @@
}
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{
- cell.setCellValue(dataFormatHandlerAdapter(value, attr));
+ cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
}
else
{
@@ -1265,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)
{
--
Gitblit v1.9.2