diff --git a/src/Spout/Reader/Helper/XLSX/SharedStringsHelper.php b/src/Spout/Reader/Helper/XLSX/SharedStringsHelper.php index 678baa6..ab33725 100644 --- a/src/Spout/Reader/Helper/XLSX/SharedStringsHelper.php +++ b/src/Spout/Reader/Helper/XLSX/SharedStringsHelper.php @@ -71,6 +71,24 @@ class SharedStringsHelper $this->tempFolder = $this->fileSystemHelper->createFolder($rootTempFolder, uniqid('sharedstrings')); } + /** + * Returns whether the XLSX file contains a shared strings XML file + * + * @return bool + */ + public function hasSharedStrings() + { + $hasSharedStrings = false; + $zip = new \ZipArchive(); + + if ($zip->open($this->filePath) === true) { + $hasSharedStrings = ($zip->locateName(self::SHARED_STRINGS_XML_FILE_PATH) !== false); + $zip->close(); + } + + return $hasSharedStrings; + } + /** * Builds an in-memory array containing all the shared strings of the worksheet. * All the strings are stored in a XML file, located at 'xl/sharedStrings.xml'. diff --git a/src/Spout/Reader/XLSX.php b/src/Spout/Reader/XLSX.php index a95be81..b0a66d1 100644 --- a/src/Spout/Reader/XLSX.php +++ b/src/Spout/Reader/XLSX.php @@ -72,8 +72,12 @@ class XLSX extends AbstractReader $this->zip = new \ZipArchive(); if ($this->zip->open($filePath) === true) { - // Extracts all the strings from the worksheets for easy access in the future - $this->extractSharedStrings($filePath); + $this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder); + + if ($this->sharedStringsHelper->hasSharedStrings()) { + // Extracts all the strings from the worksheets for easy access in the future + $this->sharedStringsHelper->extractSharedStrings(); + } // Fetch all available worksheets $this->worksheetHelper = new WorksheetHelper($filePath, $this->globalFunctionsHelper); @@ -87,19 +91,6 @@ class XLSX extends AbstractReader } } - /** - * Builds an in-memory array containing all the shared strings of the worksheets. - * - * @param string $filePath Path of the XLSX file to be read - * @return void - * @throws \Box\Spout\Common\Exception\IOException If sharedStrings XML file can't be read - */ - protected function extractSharedStrings($filePath) - { - $this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder); - $this->sharedStringsHelper->extractSharedStrings(); - } - /** * Returns whether another worksheet exists after the current worksheet. * diff --git a/tests/Spout/Reader/XLSXTest.php b/tests/Spout/Reader/XLSXTest.php index 1b4a47c..9a214f2 100644 --- a/tests/Spout/Reader/XLSXTest.php +++ b/tests/Spout/Reader/XLSXTest.php @@ -100,6 +100,20 @@ class XLSXTest extends \PHPUnit_Framework_TestCase } } + /** + * @return void + */ + public function testReadShouldSupportFilesWithoutSharedStringsFile() + { + $allRows = $this->getAllRowsForFile('sheet_with_no_shared_strings_file.xlsx'); + + $expectedRows = [ + [10, 11], + [20, 21], + ]; + $this->assertEquals($expectedRows, $allRows); + } + /** * @return void */ diff --git a/tests/resources/xlsx/sheet_with_no_shared_strings_file.xlsx b/tests/resources/xlsx/sheet_with_no_shared_strings_file.xlsx new file mode 100644 index 0000000..7870631 Binary files /dev/null and b/tests/resources/xlsx/sheet_with_no_shared_strings_file.xlsx differ