From b6cf31ea20ebe52054cc0e3c15d1e5666aa4560d Mon Sep 17 00:00:00 2001 From: anhoder Date: Sat, 18 Mar 2023 13:05:12 +0800 Subject: [PATCH] Fix incorrect font color read from xlsx Place the read font color after setting the styles --- src/PhpSpreadsheet/Reader/Xlsx.php | 41 +++++++++--------- .../Reader/Xlsx/Issue3464Test.php | 38 ++++++++++++++++ tests/data/Reader/XLSX/issue.3464.xlsx | Bin 0 -> 6118 bytes 3 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php create mode 100644 tests/data/Reader/XLSX/issue.3464.xlsx 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 0000000000000000000000000000000000000000..9a9dacc35a3be935d867c2d3e20e63e2636bdb8f GIT binary patch literal 6118 zcmaJ_2UHW?x(&T|q=QtYNpF!NsB}T3N)c(HLx2EMq$8csQT*uw(gdUm2!`HUAW}lF z(hN;n1Y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