From 1cacb57cf1cae09589948b92b195628e64a11ff6 Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Mon, 24 May 2021 19:46:11 +0100 Subject: [PATCH] add support for dates with XLSX writer (fixes #797) This adds support for writing DateTime and DateTimeImmutable values with the XLSX writer. The dates are written using the date type (t="d") which must then specify an ISO date as the value (as opposed to a linear date in previous versions). --- src/Spout/Writer/XLSX/Manager/WorksheetManager.php | 2 ++ tests/Spout/Writer/XLSX/WriterTest.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 61b93a1..4752239 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -237,6 +237,8 @@ EOD; if ($cell->isString()) { $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); + } elseif ($cell->isDate()) { + $cellXML .= ' t="d">' . $cell->getValue()->format(\DateTimeInterface::ATOM) . ''; } elseif ($cell->isBoolean()) { $cellXML .= ' t="b">' . (int) ($cell->getValue()) . ''; } elseif ($cell->isNumeric()) { diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index 26b3792..1948ffe 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -365,7 +365,7 @@ class WriterTest extends TestCase { $fileName = 'test_add_row_should_support_multiple_types_of_data.xlsx'; $dataRows = $this->createRowsFromValues([ - ['xlsx--11', true, '', 0, 10.2, null], + ['xlsx--11', true, '', 0, 10.2, null, new \DateTime('2021-01-01'), new \DateTimeImmutable('2021-02-02')], ]); $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);