]> source.dussan.org Git - poi.git/commitdiff
[bug-62181] try to handle file where shared string loading is happening twice
authorPJ Fanning <fanningpj@apache.org>
Mon, 3 Jul 2023 20:54:56 +0000 (20:54 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 3 Jul 2023 20:54:56 +0000 (20:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1910759 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/bug62181.xlsx [new file with mode: 0644]

index dd4e5ec4069d8707628f995144af3c4b5c099ba7..c48c338e6defdd8cc8780739ebc84fae089f2d83 100644 (file)
@@ -412,7 +412,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
                 if (packageReadOnly) {
                     sharedStringSource = new SharedStringsTable();
                 } else {
-                    sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
+                    List<PackagePart> matchingParts = getPackagePart().getPackage()
+                            .getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
+                    if (matchingParts.isEmpty()) {
+                        sharedStringSource = (SharedStringsTable)
+                                createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
+                    } else {
+                        sharedStringSource = new SharedStringsTable(matchingParts.get(0));
+                    }
                 }
             }
 
index d30d4d3abcaae72c9a7bab16b04318a1f167a1f1..d8d36726d073e0c287a97fc1c13c5c50d6199d76 100644 (file)
@@ -110,6 +110,7 @@ import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.eventusermodel.XSSFReader;
 import org.apache.poi.xssf.model.CalculationChain;
+import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
@@ -3854,6 +3855,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
         }
     }
 
+    @Test
+    void testBug62181() throws Exception {
+        try (XSSFWorkbook wb = openSampleWorkbook("bug62181.xlsx")) {
+            SharedStringsTable sst = wb.getSharedStringSource();
+            assertNotNull(sst);
+            assertEquals(0, sst.getCount());
+        }
+    }
+
     private static void readByCommonsCompress(File temp_excel_poi) throws IOException {
         /* read by commons-compress*/
         try (ZipFile zipFile = new ZipFile(temp_excel_poi)) {
diff --git a/test-data/spreadsheet/bug62181.xlsx b/test-data/spreadsheet/bug62181.xlsx
new file mode 100644 (file)
index 0000000..f0e0412
Binary files /dev/null and b/test-data/spreadsheet/bug62181.xlsx differ