Repository: wangguanquan/eec
Pull request #24 (2019-06-05 03:29:28)

Fix#22


Patch:
diff --git a/src/main/java/cn/ttzero/excel/reader/ExcelReader.java b/src/main/java/cn/ttzero/excel/reader/ExcelReader.java
index 3aec8682..189aaaff 100644
--- a/src/main/java/cn/ttzero/excel/reader/ExcelReader.java
+++ b/src/main/java/cn/ttzero/excel/reader/ExcelReader.java
@@ -70,6 +70,10 @@ public class ExcelReader implements AutoCloseable {
     private ExcelType type;
     private AppInfo appInfo;
 
+    /**
+     * The Shared String Table
+     */
+    private SharedStrings sst;
 
     /**
      * 实例化Reader
@@ -252,11 +256,16 @@ public class ExcelReader implements AutoCloseable {
      * @throws IOException when fail close readers
      */
     public void close() throws IOException {
-        // close sheet
+        // Close all opened sheet
         for (Sheet st : sheets) {
             st.close();
         }
-        // delete temp files
+
+        // Close Shared String Table
+        if (sst != null)
+            sst.close();
+
+        // Delete temp files
         FileUtil.rm_rf(self.toFile(), true);
         if (temp != null) {
             FileUtil.rm(temp);
@@ -313,7 +322,7 @@ public class ExcelReader implements AutoCloseable {
         Namespace ns = root.getNamespaceForPrefix("r");
 
         // Load SharedString
-        SharedStrings sst = new SharedStrings(temp.resolve("xl/sharedStrings.xml"), cacheSize, hotSize).load();
+        this.sst = new SharedStrings(temp.resolve("xl/sharedStrings.xml"), cacheSize, hotSize).load();
 
         List<Sheet> sheets = new ArrayList<>();
         @SuppressWarnings("unchecked")
diff --git a/src/main/java/cn/ttzero/excel/reader/Row.java b/src/main/java/cn/ttzero/excel/reader/Row.java
index 4086c0e1..11470e5c 100644
--- a/src/main/java/cn/ttzero/excel/reader/Row.java
+++ b/src/main/java/cn/ttzero/excel/reader/Row.java
@@ -72,7 +72,8 @@ public abstract class Row {
     }
 
     /**
-     * Returns the index of the last column (zero base)
+     * Returns the index of the last column (zero base).
+     * The last index of column is increment the max available index
      *
      * @return the last column index
      */
diff --git a/src/main/java/cn/ttzero/excel/reader/Sheet.java b/src/main/java/cn/ttzero/excel/reader/Sheet.java
index a9246bf6..0a5fa8f1 100644
--- a/src/main/java/cn/ttzero/excel/reader/Sheet.java
+++ b/src/main/java/cn/ttzero/excel/reader/Sheet.java
@@ -102,6 +102,15 @@ public interface Sheet extends AutoCloseable {
      */
     Iterator<Row> dataIterator();
 
+    /**
+     * Reset the {@link Sheet}'s row index to begging
+     *
+     * @return the unread {@link Sheet}
+     */
+    default Sheet reset() throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
     /**
      * Return a stream of all rows
      *
diff --git a/src/main/java/cn/ttzero/excel/reader/XMLRow.java b/src/main/java/cn/ttzero/excel/reader/XMLRow.java
index f4a2667a..aaca05b5 100644
--- a/src/main/java/cn/ttzero/excel/reader/XMLRow.java
+++ b/src/main/java/cn/ttzero/excel/reader/XMLRow.java
@@ -47,7 +47,8 @@ class XMLRow extends Row {
     public int getRowNumber() {
         if (index == -1)
             searchRowNumber();
-        return index;
+        // The first row index is one
+        return index - 1;
     }
 
     private XMLRow() { }
diff --git a/src/main/java/cn/ttzero/excel/reader/XMLSheet.java b/src/main/java/cn/ttzero/excel/reader/XMLSheet.java
index b322b7ac..3527b330 100644
--- a/src/main/java/cn/ttzero/excel/reader/XMLSheet.java
+++ b/src/main/java/cn/ttzero/excel/reader/XMLSheet.java
@@ -228,6 +228,7 @@ class XMLSheet implements Sheet {
             nChar++;
             eof = true;
         } else {
+            eof = false;
             sRow = new XMLRow(sst, this.startRow > 0 ? this.startRow : 1); // share row space
         }
 
@@ -383,7 +384,18 @@ class XMLSheet implements Sheet {
         if (reader != null) {
             reader.close();
         }
-        if (sst != null)
-            sst.close();
+    }
+
+    /**
+     * Reset the {@link XMLSheet}'s row index to begging
+     *
+     * @return the unread {@link XMLSheet}
+     */
+    @Override
+    public XMLSheet reset() throws IOException {
+        // Close the opening reader
+        close();
+        // Reload
+        return load();
     }
 }


Test patch:
diff --git a/src/test/java/cn/ttzero/excel/reader/ExcelReaderTest.java b/src/test/java/cn/ttzero/excel/reader/ExcelReaderTest.java
index 0aa9cb53..73b0c6e4 100644
--- a/src/test/java/cn/ttzero/excel/reader/ExcelReaderTest.java
+++ b/src/test/java/cn/ttzero/excel/reader/ExcelReaderTest.java
@@ -72,4 +72,19 @@ public class ExcelReaderTest {
             e.printStackTrace();
         }
     }
+
+    @Test public void testReset() {
+        try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) {
+
+            Sheet sheet = reader.sheet(0);
+            sheet.rows().forEach(Print::println);
+
+            println("------------------");
+
+            sheet.reset().rows().forEach(Print::println);
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 }