Browse Source

add method for #1342

Looly 5 years ago
parent
commit
71177864be

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 ### 新特性
 * 【core   】     DynaBean.create增加重载方法(pr#245@Gitee)
 * 【core   】     IdcardUtil增加重载是否忽略大小写(issue#1348@Github)
+* 【poi    】     SheetRidReader增加getRidByIndex方法(issue#1342@Github)
 
 ### Bug修复
 

+ 4 - 4
hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java

@@ -145,7 +145,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
 	}
 
 	/**
-	 * 获取指定坐标单元格,单元格不存在时返回<code>null</code>
+	 * 获取指定坐标单元格,单元格不存在时返回{@code null}
 	 *
 	 * @param locationRef 单元格地址标识符,例如A11,B5
 	 * @return {@link Cell}
@@ -157,7 +157,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
 	}
 
 	/**
-	 * 获取指定坐标单元格,单元格不存在时返回<code>null</code>
+	 * 获取指定坐标单元格,单元格不存在时返回{@code null}
 	 *
 	 * @param x X坐标,从0计数,即列号
 	 * @param y Y坐标,从0计数,即行号
@@ -193,7 +193,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
 	}
 
 	/**
-	 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回<code>null</code>
+	 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null}
 	 *
 	 * @param locationRef        单元格地址标识符,例如A11,B5
 	 * @param isCreateIfNotExist 单元格不存在时是否创建
@@ -206,7 +206,7 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
 	}
 
 	/**
-	 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回<code>null</code>
+	 * 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回{@code null}
 	 *
 	 * @param x                  X坐标,从0计数,即列号
 	 * @param y                  Y坐标,从0计数,即行号

+ 1 - 1
hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java

@@ -233,7 +233,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
 	/**
 	 * 重命名sheet
 	 *
-	 * @param sheet     sheet需要,0表示第一个sheet
+	 * @param sheet     sheet序号,0表示第一个sheet
 	 * @param sheetName 新的sheet名
 	 * @return this
 	 * @since 4.1.8

+ 34 - 7
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<Integer, Integer> ID_RID_MAP = new HashMap<>();
-	private final Map<String, Integer> NAME_RID_MAP = new HashMap<>();
+	private final Map<Integer, Integer> ID_RID_MAP = new LinkedHashMap<>();
+	private final Map<String, Integer> 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);
 			}
 		}