diff --git a/CHANGELOG.md b/CHANGELOG.md index 83b637af7..92fa60e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题(issue#1581@Github) * 【core 】 修复NumberUtil.decimalFormat参数传错的问题(issue#I3SDS3@Gitee) * 【json 】 修复JSONArray.put方法不能覆盖值的问题 +* 【poi 】 修复sax方式读取xls无法根据sheet名称获取数据(issue#I3S4NH@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.6.5 (2021-05-08) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java index ef10fe11d..fad6d5da6 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java @@ -96,18 +96,18 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader 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 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 } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxReader.java index 0895e4d29..01fe55749 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxReader.java @@ -22,21 +22,21 @@ public interface ExcelSaxReader { * 开始读取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 * @throws POIException POI异常 */ - T read(File file, String idOrRid) throws POIException; + T read(File file, String idOrRidOrSheetName) throws POIException; /** * 开始读取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 * @throws POIException POI异常 */ - T read(InputStream in, String idOrRid) throws POIException; + T read(InputStream in, String idOrRidOrSheetName) throws POIException; /** * 开始读取Excel,读取所有sheet