{
+
+ /**
+ * 开始读取Excel
+ *
+ * @param file Excel文件
+ * @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
+ * @return this
+ * @throws POIException POI异常
+ */
+ T read(File file, String idOrRid) throws POIException;
+
+ /**
+ * 开始读取Excel,读取结束后并不关闭流
+ *
+ * @param in Excel流
+ * @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
+ * @return this
+ * @throws POIException POI异常
+ */
+ T read(InputStream in, String idOrRid) throws POIException;
+
/**
* 开始读取Excel,读取所有sheet
- *
+ *
* @param path Excel文件路径
* @return this
* @throws POIException POI异常
*/
- T read(String path) throws POIException;
+ default T read(String path) throws POIException {
+ return read(FileUtil.file(path));
+ }
/**
* 开始读取Excel,读取所有sheet
- *
+ *
* @param file Excel文件
* @return this
* @throws POIException POI异常
*/
- T read(File file) throws POIException;
+ default T read(File file) throws POIException {
+ return read(file, -1);
+ }
/**
* 开始读取Excel,读取所有sheet,读取结束后并不关闭流
- *
+ *
* @param in Excel包流
* @return this
* @throws POIException POI异常
*/
- T read(InputStream in) throws POIException;
+ default T read(InputStream in) throws POIException {
+ return read(in, -1);
+ }
/**
* 开始读取Excel
- *
+ *
* @param path 文件路径
* @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
* @return this
* @throws POIException POI异常
*/
- T read(String path, int rid) throws POIException;
+ default T read(String path, int rid) throws POIException {
+ return read(FileUtil.file(path), rid);
+ }
/**
* 开始读取Excel
- *
+ *
+ * @param path 文件路径
+ * @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
+ * @return this
+ * @throws POIException POI异常
+ */
+ default T read(String path, String rid) throws POIException {
+ return read(FileUtil.file(path), rid);
+ }
+
+ /**
+ * 开始读取Excel
+ *
* @param file Excel文件
* @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
* @return this
* @throws POIException POI异常
*/
- T read(File file, int rid) throws POIException;
+ default T read(File file, int rid) throws POIException{
+ return read(file, String.valueOf(rid));
+ }
/**
* 开始读取Excel,读取结束后并不关闭流
- *
+ *
* @param in Excel流
* @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
* @return this
* @throws POIException POI异常
*/
- T read(InputStream in, int rid) throws POIException;
+ default T read(InputStream in, int rid) throws POIException{
+ return read(in, String.valueOf(rid));
+ };
}
diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java
index b8acb25ce..52b13ca49 100644
--- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java
+++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java
@@ -73,9 +73,9 @@ public class ExcelSaxUtil {
}
break;
case NUMBER:
- try{
+ try {
result = getNumberValue(value, numFmtString);
- }catch (NumberFormatException e){
+ } catch (NumberFormatException e) {
result = value;
}
break;
@@ -150,6 +150,7 @@ public class ExcelSaxUtil {
public static void readFrom(InputStream xmlDocStream, ContentHandler handler) throws DependencyException, POIException, IORuntimeException {
XMLReader xmlReader;
try {
+// xmlReader = XMLReaderFactory.createXMLReader();
//noinspection deprecation
xmlReader = SAXHelper.newXMLReader();
} catch (SAXException | ParserConfigurationException e) {
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
new file mode 100644
index 000000000..7af3c4884
--- /dev/null
+++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/SheetRidReader.java
@@ -0,0 +1,110 @@
+package cn.hutool.poi.excel.sax;
+
+import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.poi.exceptions.POIException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.xssf.eventusermodel.XSSFReader;
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.DefaultHandler;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 在Sax方式读取Excel时,读取sheet标签中sheetId和rid的对应关系,类似于:
+ *
+ * <sheet name="Sheet6" sheetId="4" r:id="6"/>
+ *
+ *
+ * 读取结果为:
+ *
+ *
+ * {"4": "6"}
+ *
+ *
+ * @author looly
+ * @since 5.4.4
+ */
+public class SheetRidReader extends DefaultHandler {
+
+ private final static String TAG_NAME = "sheet";
+ private final static String RID_ATTR = "r:id";
+ 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<>();
+
+ /**
+ * 读取Wordkbook的XML中sheet标签中sheetId和rid的对应关系
+ *
+ * @param xssfReader XSSF读取器
+ */
+ public SheetRidReader read(XSSFReader xssfReader){
+ InputStream workbookData = null;
+ try{
+ workbookData = xssfReader.getWorkbookData();
+ ExcelSaxUtil.readFrom(workbookData, this);
+ } catch (InvalidFormatException e) {
+ throw new POIException(e);
+ } catch (IOException e) {
+ throw new IORuntimeException(e);
+ } finally {
+ IoUtil.close(workbookData);
+ }
+ return this;
+ }
+
+ /**
+ * 根据sheetId获取rid
+ *
+ * @param sheetId Sheet的ID
+ * @return rid
+ */
+ public String getRidBySheetId(String sheetId){
+ return ID_RID_MAP.get(sheetId);
+ }
+
+ /**
+ * 根据sheet name获取rid
+ *
+ * @param sheetName Sheet的name
+ * @return rid
+ */
+ public String getRidByName(String sheetName){
+ return NAME_RID_MAP.get(sheetName);
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes) {
+ if(TAG_NAME.equalsIgnoreCase(localName)){
+ final int length = attributes.getLength();
+ String sheetId = null;
+ String rid = null;
+ String name = null;
+ for (int i = 0; i < length; i++) {
+ switch (attributes.getLocalName(i)){
+ case SHEET_ID_ATTR:
+ sheetId = attributes.getValue(i);
+ break;
+ case RID_ATTR:
+ rid = attributes.getValue(i);
+ break;
+ case NAME_ATTR:
+ name = attributes.getValue(i);
+ break;
+ }
+ if(StrUtil.isNotEmpty(sheetId)){
+ ID_RID_MAP.put(sheetId, rid);
+ }
+ if(StrUtil.isNotEmpty(name)){
+ NAME_RID_MAP.put(name, rid);
+ }
+ }
+ }
+ }
+}