Merge pull request #2804 from PHPOffice/Performance-Xlsx-Writer

Memory Experiment in Xlsx Writer
This commit is contained in:
Mark Baker 2022-05-05 13:49:11 +02:00 committed by GitHub
commit b72b423309
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 12 deletions

View File

@ -5250,11 +5250,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
-
message: "#^Offset int\\<1, max\\> on array\\<string, non\\-empty\\-array\\<int, string\\>\\> in isset\\(\\) does not exist\\.$#"
count: 2
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
- -
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#" message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#"
count: 1 count: 1

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx; namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\RichText\Run; use PhpOffice\PhpSpreadsheet\RichText\Run;
@ -35,8 +36,9 @@ class StringTable extends WriterPart
$aFlippedStringTable = $this->flipStringTable($aStringTable); $aFlippedStringTable = $this->flipStringTable($aStringTable);
// Loop through cells // Loop through cells
foreach ($worksheet->getCoordinates() as $coordinate) { foreach ($worksheet->getCellCollection()->getCoordinates() as $coordinate) {
$cell = $worksheet->getCell($coordinate); /** @var Cell $cell */
$cell = $worksheet->getCellCollection()->get($coordinate);
$cellValue = $cell->getValue(); $cellValue = $cell->getValue();
if ( if (
!is_object($cellValue) && !is_object($cellValue) &&

View File

@ -1145,11 +1145,15 @@ class Worksheet extends WriterPart
// Highest row number // Highest row number
$highestRow = $worksheet->getHighestRow(); $highestRow = $worksheet->getHighestRow();
// Loop through cells // Loop through cells building a comma-separated list of the columns in each row
// This is a trade-off between the memory usage that is required for a full array of columns,
// and execution speed
/** @var array<int, string> $cellsByRow */
$cellsByRow = []; $cellsByRow = [];
foreach ($worksheet->getCoordinates() as $coordinate) { foreach ($worksheet->getCoordinates() as $coordinate) {
$cellAddress = Coordinate::coordinateFromString($coordinate); [$column, $row] = Coordinate::coordinateFromString($coordinate);
$cellsByRow[$cellAddress[1]][] = $coordinate; $cellsByRow[$row] = $cellsByRow[$row] ?? '';
$cellsByRow[$row] .= "{$column},";
} }
$currentRow = 0; $currentRow = 0;
@ -1195,9 +1199,12 @@ class Worksheet extends WriterPart
// Write cells // Write cells
if (isset($cellsByRow[$currentRow])) { if (isset($cellsByRow[$currentRow])) {
foreach ($cellsByRow[$currentRow] as $cellAddress) { // We have a comma-separated list of column names (with a trailing entry); split to an array
$columnsInRow = explode(',', $cellsByRow[$currentRow]);
array_pop($columnsInRow);
foreach ($columnsInRow as $column) {
// Write cell // Write cell
$this->writeCell($objWriter, $worksheet, $cellAddress, $aFlippedStringTable); $this->writeCell($objWriter, $worksheet, "{$column}{$currentRow}", $aFlippedStringTable);
} }
} }