mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复当sheetName 不存在时,ExcelUtil.getReader方法不会释放文件问题
This commit is contained in:
parent
d87bf2dfd9
commit
016b6ef810
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.26(2024-01-26)
|
||||
# 5.8.26(2024-02-03)
|
||||
|
||||
### 🐣新特性
|
||||
* 【db 】 RedisDS增加user支持(issue#I8XEQ4@Gitee)
|
||||
@ -14,6 +14,7 @@
|
||||
* 【extra 】 修复SshjSftpSession关闭导致的问题(issue#3472@Github)
|
||||
* 【http 】 修复HtmlUtil.removeHtmlAttr处理空格问题(issue#I8YV0K@Gitee)
|
||||
* 【core 】 修复CollUtil.containsAll在coll2长度大于coll1时逻辑歧义问题(issue#I8Z2Q4@Gitee)
|
||||
* 【poi 】 修复当sheetName 不存在时,ExcelUtil.getReader方法不会释放文件问题(issue#I8ZIQC@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.25(2024-01-11)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.poi.excel;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.poi.excel.cell.CellEditor;
|
||||
import cn.hutool.poi.excel.cell.CellHandler;
|
||||
@ -110,7 +111,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
* @param sheetIndex sheet序号,0表示第一个sheet
|
||||
*/
|
||||
public ExcelReader(Workbook book, int sheetIndex) {
|
||||
this(book.getSheetAt(sheetIndex));
|
||||
this(getSheetOrCloseWorkbook(book, sheetIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -120,7 +121,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
* @param sheetName sheet名,第一个默认是sheet1
|
||||
*/
|
||||
public ExcelReader(Workbook book, String sheetName) {
|
||||
this(book.getSheet(sheetName));
|
||||
this(getSheetOrCloseWorkbook(book, sheetName));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -451,5 +452,50 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
private void checkNotClosed() {
|
||||
Assert.isFalse(this.isClosed, "ExcelReader has been closed!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Sheet,如果不存在则关闭{@link Workbook}并抛出异常,解决当sheet不存在时,文件依旧被占用问题<br>
|
||||
* 见:Issue#I8ZIQC
|
||||
* @param workbook {@link Workbook},非空
|
||||
* @param name sheet名称,不存在抛出异常
|
||||
* @return {@link Sheet}
|
||||
* @throws IllegalArgumentException workbook为空或sheet不能存在
|
||||
*/
|
||||
private static Sheet getSheetOrCloseWorkbook(final Workbook workbook, String name) throws IllegalArgumentException{
|
||||
Assert.notNull(workbook);
|
||||
if(null == name){
|
||||
name = "sheet1";
|
||||
}
|
||||
final Sheet sheet = workbook.getSheet(name);
|
||||
if(null == sheet){
|
||||
IoUtil.close(workbook);
|
||||
throw new IllegalArgumentException("Sheet [" + name + "] not exist!");
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Sheet,如果不存在则关闭{@link Workbook}并抛出异常,解决当sheet不存在时,文件依旧被占用问题<br>
|
||||
* 见:Issue#I8ZIQC
|
||||
* @param workbook {@link Workbook},非空
|
||||
* @param sheetIndex sheet index
|
||||
* @return {@link Sheet}
|
||||
* @throws IllegalArgumentException workbook为空或sheet不能存在
|
||||
*/
|
||||
private static Sheet getSheetOrCloseWorkbook(final Workbook workbook, final int sheetIndex) throws IllegalArgumentException{
|
||||
Assert.notNull(workbook);
|
||||
final Sheet sheet;
|
||||
try {
|
||||
sheet = workbook.getSheetAt(sheetIndex);
|
||||
} catch (final IllegalArgumentException e){
|
||||
IoUtil.close(workbook);
|
||||
throw e;
|
||||
}
|
||||
if(null == sheet){
|
||||
IoUtil.close(workbook);
|
||||
throw new IllegalArgumentException("Sheet at [" + sheetIndex + "] not exist!");
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
// ------------------------------------------------------------------------------------------------------- Private methods end
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user