From 1c0d3ae59dc0c16a8c27392fe2687ee9ce6f2130 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Fri, 22 Jan 2021 11:41:16 +0100 Subject: [PATCH] Fix Xlsx reader overriding manually set number format with builtin number format Relevant spreadsheet: https://circabc.europa.eu/ui/group/0e5f18c2-4b2f-42e9-aed4-dfe50ae1263b/library/3bbc2ffd-d4c1-4cbd-b8e5-71aa44f6ed3c/details Verified by running: ``` var_dump($worksheet->getCell('E2')->getFormattedValue()); ``` ... which should output `2`, not some garbled mess --- src/PhpSpreadsheet/Reader/Xlsx.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 124cc3b252..4222b95422 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -488,7 +488,7 @@ public function load($pFilename) } if (!$this->readDataOnly && $xmlStyles) { foreach ($xmlStyles->cellXfs->xf as $xf) { - $numFmt = NumberFormat::FORMAT_GENERAL; + $numFmt = null; if ($xf['numFmtId']) { if (isset($numFmts)) { @@ -503,6 +503,7 @@ public function load($pFilename) // But there's a lot of naughty homebrew xlsx writers that do use "reserved" id values that aren't actually used // So we make allowance for them rather than lose formatting masks if ( + $numFmt === null && (int) $xf['numFmtId'] < 164 && NumberFormat::builtInFormatCode((int) $xf['numFmtId']) !== '' ) { @@ -515,7 +516,7 @@ public function load($pFilename) } $style = (object) [ - 'numFmt' => $numFmt, + 'numFmt' => $numFmt === null ? NumberFormat::FORMAT_GENERAL : $numFmt, 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])], 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])], 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])],