From 71177864be08a6c21ca9a10abb3ae434113e7de4 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 2 Jan 2021 11:36:14 +0800 Subject: [PATCH] add method for #1342 --- CHANGELOG.md | 1 + .../java/cn/hutool/poi/excel/ExcelBase.java | 8 ++-- .../java/cn/hutool/poi/excel/ExcelWriter.java | 2 +- .../hutool/poi/excel/sax/SheetRidReader.java | 41 +++++++++++++++---- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca0a194e6..ff4b20fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 新特性 * 【core 】 DynaBean.create增加重载方法(pr#245@Gitee) * 【core 】 IdcardUtil增加重载是否忽略大小写(issue#1348@Github) +* 【poi 】 SheetRidReader增加getRidByIndex方法(issue#1342@Github) ### Bug修复 diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java index c9d191ae8..90da8bd7d 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java @@ -145,7 +145,7 @@ public class ExcelBase> implements Closeable { } /** - * 获取指定坐标单元格,单元格不存在时返回null + * 获取指定坐标单元格,单元格不存在时返回{@code null} * * @param locationRef 单元格地址标识符,例如A11,B5 * @return {@link Cell} @@ -157,7 +157,7 @@ public class ExcelBase> implements Closeable { } /** - * 获取指定坐标单元格,单元格不存在时返回null + * 获取指定坐标单元格,单元格不存在时返回{@code null} * * @param x X坐标,从0计数,即列号 * @param y Y坐标,从0计数,即行号 @@ -193,7 +193,7 @@ public class ExcelBase> implements Closeable { } /** - * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null + * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null} * * @param locationRef 单元格地址标识符,例如A11,B5 * @param isCreateIfNotExist 单元格不存在时是否创建 @@ -206,7 +206,7 @@ public class ExcelBase> implements Closeable { } /** - * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null + * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null} * * @param x X坐标,从0计数,即列号 * @param y Y坐标,从0计数,即行号 diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index 46dda3f2c..6585b78ab 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -233,7 +233,7 @@ public class ExcelWriter extends ExcelBase { /** * 重命名sheet * - * @param sheet sheet需要,0表示第一个sheet + * @param sheet sheet序号,0表示第一个sheet * @param sheetName 新的sheet名 * @return this * @since 4.1.8 diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/SheetRidReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/SheetRidReader.java index 65d697be5..530c3f762 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/SheetRidReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/SheetRidReader.java @@ -1,5 +1,6 @@ package cn.hutool.poi.excel.sax; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; @@ -11,7 +12,7 @@ import org.xml.sax.helpers.DefaultHandler; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -36,8 +37,8 @@ public class SheetRidReader extends DefaultHandler { private final static String SHEET_ID_ATTR = "sheetId"; private final static String NAME_ATTR = "name"; - private final Map ID_RID_MAP = new HashMap<>(); - private final Map NAME_RID_MAP = new HashMap<>(); + private final Map ID_RID_MAP = new LinkedHashMap<>(); + private final Map NAME_RID_MAP = new LinkedHashMap<>(); /** * 读取Wordkbook的XML中sheet标签中sheetId和rid的对应关系 @@ -79,7 +80,7 @@ public class SheetRidReader extends DefaultHandler { */ public Integer getRidBySheetIdBase0(int sheetId) { final Integer rid = getRidBySheetId(sheetId + 1); - if(null != rid){ + if (null != rid) { return rid - 1; } return null; @@ -104,7 +105,33 @@ public class SheetRidReader extends DefaultHandler { */ public Integer getRidByNameBase0(String sheetName) { final Integer rid = getRidByName(sheetName); - if(null != rid){ + if (null != rid) { + return rid - 1; + } + return null; + } + + /** + * 通过sheet的序号获取rid + * + * @param index 序号,从0开始 + * @return rid + * @since 5.5.7 + */ + public Integer getRidByIndex(int index) { + return CollUtil.get(this.NAME_RID_MAP.values(), index); + } + + /** + * 通过sheet的序号获取rid + * + * @param index 序号,从0开始 + * @return rid,从0开始 + * @since 5.5.7 + */ + public Integer getRidByIndexBase0(int index) { + final Integer rid = CollUtil.get(this.NAME_RID_MAP.values(), index); + if (null != rid) { return rid - 1; } return null; @@ -114,7 +141,7 @@ public class SheetRidReader extends DefaultHandler { public void startElement(String uri, String localName, String qName, Attributes attributes) { if (TAG_NAME.equalsIgnoreCase(localName)) { final String ridStr = attributes.getValue(RID_ATTR); - if(StrUtil.isEmpty(ridStr)){ + if (StrUtil.isEmpty(ridStr)) { return; } final int rid = Integer.parseInt(StrUtil.removePrefixIgnoreCase(ridStr, Excel07SaxReader.RID_PREFIX)); @@ -127,7 +154,7 @@ public class SheetRidReader extends DefaultHandler { // sheetId和rid映射 final String sheetIdStr = attributes.getValue(SHEET_ID_ATTR); - if(StrUtil.isNotEmpty(sheetIdStr)){ + if (StrUtil.isNotEmpty(sheetIdStr)) { ID_RID_MAP.put(Integer.parseInt(sheetIdStr), rid); } }