mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
fix read by sheetName bug
This commit is contained in:
parent
edd0449187
commit
c5887344e9
@ -21,6 +21,7 @@
|
|||||||
* 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题(issue#1581@Github)
|
* 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题(issue#1581@Github)
|
||||||
* 【core 】 修复NumberUtil.decimalFormat参数传错的问题(issue#I3SDS3@Gitee)
|
* 【core 】 修复NumberUtil.decimalFormat参数传错的问题(issue#I3SDS3@Gitee)
|
||||||
* 【json 】 修复JSONArray.put方法不能覆盖值的问题
|
* 【json 】 修复JSONArray.put方法不能覆盖值的问题
|
||||||
|
* 【poi 】 修复sax方式读取xls无法根据sheet名称获取数据(issue#I3S4NH@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.6.5 (2021-05-08)
|
# 5.6.5 (2021-05-08)
|
||||||
|
@ -96,18 +96,18 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
|||||||
|
|
||||||
// ------------------------------------------------------------------------------ Read start
|
// ------------------------------------------------------------------------------ Read start
|
||||||
@Override
|
@Override
|
||||||
public Excel03SaxReader read(File file, String idOrRid) throws POIException {
|
public Excel03SaxReader read(File file, String idOrRidOrSheetName) throws POIException {
|
||||||
try {
|
try {
|
||||||
return read(new POIFSFileSystem(file), idOrRid);
|
return read(new POIFSFileSystem(file), idOrRidOrSheetName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Excel03SaxReader read(InputStream excelStream, String idOrRid) throws POIException {
|
public Excel03SaxReader read(InputStream excelStream, String idOrRidOrSheetName) throws POIException {
|
||||||
try {
|
try {
|
||||||
return read(new POIFSFileSystem(excelStream), idOrRid);
|
return read(new POIFSFileSystem(excelStream), idOrRidOrSheetName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
}
|
}
|
||||||
@ -117,12 +117,12 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
|||||||
* 读取
|
* 读取
|
||||||
*
|
*
|
||||||
* @param fs {@link POIFSFileSystem}
|
* @param fs {@link POIFSFileSystem}
|
||||||
* @param id sheet序号,从0开始
|
* @param idOrRidOrSheetName sheet id或者rid编号或sheet名称,从0开始,rid必须加rId前缀,例如rId0,如果为-1处理所有编号的sheet
|
||||||
* @return this
|
* @return this
|
||||||
* @throws POIException IO异常包装
|
* @throws POIException IO异常包装
|
||||||
*/
|
*/
|
||||||
public Excel03SaxReader read(POIFSFileSystem fs, String id) throws POIException {
|
public Excel03SaxReader read(POIFSFileSystem fs, String idOrRidOrSheetName) throws POIException {
|
||||||
this.rid = getSheetIndex(id);
|
this.rid = getSheetIndex(idOrRidOrSheetName);
|
||||||
|
|
||||||
formatListener = new FormatTrackingHSSFListener(new MissingRecordAwareHSSFListener(this));
|
formatListener = new FormatTrackingHSSFListener(new MissingRecordAwareHSSFListener(this));
|
||||||
final HSSFRequest request = new HSSFRequest();
|
final HSSFRequest request = new HSSFRequest();
|
||||||
@ -365,9 +365,32 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
|||||||
final int sheetIndex;
|
final int sheetIndex;
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(idOrRidOrSheetName);
|
return Integer.parseInt(idOrRidOrSheetName);
|
||||||
} catch (NumberFormatException ignore) {
|
} catch (NumberFormatException e) {
|
||||||
throw new IllegalArgumentException("Invalid sheet id: " + idOrRidOrSheetName);
|
// 非数字,可能为sheet名称
|
||||||
|
sheetIndex = getSheetIndexByName(idOrRidOrSheetName);
|
||||||
|
if(sheetIndex > 0){
|
||||||
|
return sheetIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid rId or id or sheetName: " + idOrRidOrSheetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过sheet名称获取其位置(rid)
|
||||||
|
* @param sheetName sheet名称
|
||||||
|
* @return 位置,-1表示未找到
|
||||||
|
* @since 5.6.6
|
||||||
|
*/
|
||||||
|
private int getSheetIndexByName(String sheetName){
|
||||||
|
final List<BoundSheetRecord> boundSheetRecords = this.boundSheetRecords;
|
||||||
|
final int size = boundSheetRecords.size();
|
||||||
|
for(int i = 0; i < size; i++){
|
||||||
|
if(StrUtil.equals(sheetName, boundSheetRecords.get(i).getSheetname())){
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
// ---------------------------------------------------------------------------------------------- Private method end
|
// ---------------------------------------------------------------------------------------------- Private method end
|
||||||
}
|
}
|
||||||
|
@ -22,21 +22,21 @@ public interface ExcelSaxReader<T> {
|
|||||||
* 开始读取Excel
|
* 开始读取Excel
|
||||||
*
|
*
|
||||||
* @param file Excel文件
|
* @param file Excel文件
|
||||||
* @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号或sheet名称,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||||
* @return this
|
* @return this
|
||||||
* @throws POIException POI异常
|
* @throws POIException POI异常
|
||||||
*/
|
*/
|
||||||
T read(File file, String idOrRid) throws POIException;
|
T read(File file, String idOrRidOrSheetName) throws POIException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始读取Excel,读取结束后并不关闭流
|
* 开始读取Excel,读取结束后并不关闭流
|
||||||
*
|
*
|
||||||
* @param in Excel流
|
* @param in Excel流
|
||||||
* @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||||
* @return this
|
* @return this
|
||||||
* @throws POIException POI异常
|
* @throws POIException POI异常
|
||||||
*/
|
*/
|
||||||
T read(InputStream in, String idOrRid) throws POIException;
|
T read(InputStream in, String idOrRidOrSheetName) throws POIException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始读取Excel,读取所有sheet
|
* 开始读取Excel,读取所有sheet
|
||||||
|
Loading…
Reference in New Issue
Block a user