From 2f6193ce20ad0429ca3c7d7d94fbce9c199e2de0 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 10 Nov 2015 17:17:50 -0800 Subject: [PATCH] [XLSX] Skip empty cells on write Since cells are referenced by their coordinates (A2, B4...), it is not necessary to write empty cells. This will reduce the final size of the generated XML and therefore XLSX file. --- src/Spout/Writer/XLSX/Internal/Worksheet.php | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index be67e19..b78748c 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -131,36 +131,38 @@ EOD; $rowIndex = $this->lastWrittenRowIndex + 1; $numCells = count($dataRow); - $data = ''; + $rowXML = ''; foreach($dataRow as $cellValue) { $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); - $data .= 'getId() . '"'; + $cellXML = 'getId() . '"'; if (CellHelper::isNonEmptyString($cellValue)) { if ($this->shouldUseInlineStrings) { - $data .= ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . ''; + $cellXML .= ' t="inlineStr">' . $this->stringsEscaper->escape($cellValue) . ''; } else { $sharedStringId = $this->sharedStringsHelper->writeString($cellValue); - $data .= ' t="s">' . $sharedStringId . ''; + $cellXML .= ' t="s">' . $sharedStringId . ''; } } else if (CellHelper::isBoolean($cellValue)) { - $data .= ' t="b">' . $cellValue . ''; + $cellXML .= ' t="b">' . $cellValue . ''; } else if (CellHelper::isNumeric($cellValue)) { - $data .= '>' . $cellValue . ''; + $cellXML .= '>' . $cellValue . ''; } else if (empty($cellValue)) { - $data .= '/>'; + // don't write empty cells (not appending to $cellXML is the right behavior!) + $cellXML = ''; } else { throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue)); } + $rowXML .= $cellXML; $cellNumber++; } - $data .= ''; + $rowXML .= ''; - $wasWriteSuccessful = fwrite($this->sheetFilePointer, $data); + $wasWriteSuccessful = fwrite($this->sheetFilePointer, $rowXML); if ($wasWriteSuccessful === false) { throw new IOException("Unable to write data in {$this->worksheetFilePath}"); }