From: PJ Fanning Date: Mon, 3 Jul 2023 20:54:56 +0000 (+0000) Subject: [bug-62181] try to handle file where shared string loading is happening twice X-Git-Tag: REL_5_2_4~120 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=04442923f32894da8d8496145fed186ab1b4fdc4;p=poi.git [bug-62181] try to handle file where shared string loading is happening twice git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1910759 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index dd4e5ec406..c48c338e6d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 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)); + } } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index d30d4d3abc..d8d36726d0 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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 index 0000000000..f0e04126b3 Binary files /dev/null and b/test-data/spreadsheet/bug62181.xlsx differ