diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 4d8a4933f7..0786517622 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -507,26 +507,6 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet $relsWorkbook = $this->loadZip("$dir/_rels/" . basename($relTarget) . '.rels', ''); $relsWorkbook->registerXPathNamespace('rel', Namespaces::RELATIONSHIPS); - $sharedStrings = []; - $relType = "rel:Relationship[@Type='" - //. Namespaces::SHARED_STRINGS - . "$xmlNamespaceBase/sharedStrings" - . "']"; - $xpath = self::getArrayItem($relsWorkbook->xpath($relType)); - - if ($xpath) { - $xmlStrings = $this->loadZip("$dir/$xpath[Target]", $mainNS); - if (isset($xmlStrings->si)) { - foreach ($xmlStrings->si as $val) { - if (isset($val->t)) { - $sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t); - } elseif (isset($val->r)) { - $sharedStrings[] = $this->parseRichText($val); - } - } - } - } - $worksheets = []; $macros = $customUI = null; foreach ($relsWorkbook->Relationship as $elex) { @@ -682,6 +662,27 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet $dxfs = $this->styleReader->dxfs($this->readDataOnly); $styles = $this->styleReader->styles(); + // Read content after setting the styles + $sharedStrings = []; + $relType = "rel:Relationship[@Type='" + //. Namespaces::SHARED_STRINGS + . "$xmlNamespaceBase/sharedStrings" + . "']"; + $xpath = self::getArrayItem($relsWorkbook->xpath($relType)); + + if ($xpath) { + $xmlStrings = $this->loadZip("$dir/$xpath[Target]", $mainNS); + if (isset($xmlStrings->si)) { + foreach ($xmlStrings->si as $val) { + if (isset($val->t)) { + $sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t); + } elseif (isset($val->r)) { + $sharedStrings[] = $this->parseRichText($val); + } + } + } + } + $xmlWorkbook = $this->loadZipNoNamespace($relTarget, $mainNS); $xmlWorkbookNS = $this->loadZip($relTarget, $mainNS); diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php new file mode 100644 index 0000000000..f08af48639 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php @@ -0,0 +1,38 @@ +setReadEmptyCells(false); + + $sheet = $objReader->load(self::$testbook)->getActiveSheet(); + $rickText = $sheet->getCell([1, 1])->getValue(); + self::assertInstanceOf(RichText::class, $rickText); + + $elements = $rickText->getRichTextElements(); + self::assertCount(2, $elements); + + self::assertEquals("产品介绍\n", $elements[0]->getText()); + $font = $elements[0]->getFont(); + self::assertNotNull($font); + self::assertEquals('7f7f7f', $font->getColor()->getRGB()); + + self::assertEquals('(这是一行示例数据,在导入时需要删除该行)', $elements[1]->getText()); + $font = $elements[1]->getFont(); + self::assertNotNull($font); + self::assertEquals('ff2600', $font->getColor()->getRGB()); + } +} diff --git a/tests/data/Reader/XLSX/issue.3464.xlsx b/tests/data/Reader/XLSX/issue.3464.xlsx new file mode 100644 index 0000000000..9a9dacc35a Binary files /dev/null and b/tests/data/Reader/XLSX/issue.3464.xlsx differ