From c3f26a2d693478f0d568ecbbb8353f24fd5183bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Dobro=C5=88?= Date: Fri, 17 Nov 2023 12:28:06 +0100 Subject: [PATCH 1/4] Add colspan/rowspan support --- lib/HtmlPhpExcel/HtmlPhpExcel.php | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/HtmlPhpExcel/HtmlPhpExcel.php b/lib/HtmlPhpExcel/HtmlPhpExcel.php index 11bebc4..d2181f1 100644 --- a/lib/HtmlPhpExcel/HtmlPhpExcel.php +++ b/lib/HtmlPhpExcel/HtmlPhpExcel.php @@ -4,7 +4,9 @@ namespace Ticketpark\HtmlPhpExcel; +use avadim\FastExcelHelper\Helper; use avadim\FastExcelWriter\Excel; +use avadim\FastExcelWriter\Sheet; use Ticketpark\HtmlPhpExcel\Elements as HtmlPhpExcelElement; use Ticketpark\HtmlPhpExcel\Exception\InexistentExcelObjectException; use Ticketpark\HtmlPhpExcel\Parser\Parser; @@ -158,6 +160,15 @@ private function createExcel(): void // Loop over all cells in a row $colIndex = 1; foreach($row->getCells() as $cell) { + $excelCellIndex = Helper::colLetter($colIndex).$rowIndex; + + // Skip cells withing merge range + while ($this->isMerged($sheet, $excelCellIndex)) { + $colIndex++; + $excelCellIndex = Helper::colLetter($colIndex).$rowIndex; + $sheet->cell($excelCellIndex); + } + $cellStyles = $this->getStyles($cell); $sheet->writeCell( trim($cell->getValue()), @@ -177,6 +188,23 @@ private function createExcel(): void $sheet->addNote(Excel::cellAddress($rowIndex, $colIndex), $cellComment); } + $colspan = $cell->getAttribute('colspan'); + $rowspan = $cell->getAttribute('rowspan'); + + if ($colspan || $rowspan) { + if ($colspan) { + $colspan = $colspan - 1; + } + + if ($rowspan) { + $rowspan = $rowspan - 1; + } + + $mergeCellsTargetCellIndex = Helper::colLetter($colIndex + $colspan).($rowIndex + $rowspan); + + $sheet->mergeCells($excelCellIndex.':'.$mergeCellsTargetCellIndex); + } + $colIndex++; } @@ -186,6 +214,17 @@ private function createExcel(): void } } + private function isMerged(Sheet $sheet, string $cellAddress): bool + { + foreach ($sheet->getMergedCells() as $range) { + if (Helper::inRange($cellAddress, $range)) { + return true; + } + } + + return false; + } + private function getStyles(HtmlPhpExcelElement\Element $documentElement): array { $styles = []; From 5fc74f2a597da09707db5d682e337978dd216ff0 Mon Sep 17 00:00:00 2001 From: Manuel Reinhard Date: Thu, 23 Nov 2023 20:46:05 +0100 Subject: [PATCH 2/4] Define dependency version to enable cell merging --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 2eba8a0..c0286f1 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "ext-json": "*", "ext-dom": "*", "ext-intl": "*", + "avadim/fast-excel-helper": "^1.0.4", "avadim/fast-excel-writer": "^4.5" }, "require-dev": { From 54080952ba2cbb655bef056ef193821c6fe01ce4 Mon Sep 17 00:00:00 2001 From: Manuel Reinhard Date: Thu, 23 Nov 2023 20:46:15 +0100 Subject: [PATCH 3/4] Add cell merging example --- example/example.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/example/example.html b/example/example.html index 6d8d462..5df12ff 100644 --- a/example/example.html +++ b/example/example.html @@ -44,8 +44,9 @@ - - + + Merge cells + No need to fill all cells. And no need to care about how many cells there are in each row. From b0061534bab73c3ca9e5e555161d45875f7d5bb0 Mon Sep 17 00:00:00 2001 From: Manuel Reinhard Date: Thu, 23 Nov 2023 20:46:25 +0100 Subject: [PATCH 4/4] Refactor cell merging --- lib/HtmlPhpExcel/HtmlPhpExcel.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/HtmlPhpExcel/HtmlPhpExcel.php b/lib/HtmlPhpExcel/HtmlPhpExcel.php index d2181f1..11527df 100644 --- a/lib/HtmlPhpExcel/HtmlPhpExcel.php +++ b/lib/HtmlPhpExcel/HtmlPhpExcel.php @@ -160,15 +160,16 @@ private function createExcel(): void // Loop over all cells in a row $colIndex = 1; foreach($row->getCells() as $cell) { - $excelCellIndex = Helper::colLetter($colIndex).$rowIndex; - // Skip cells withing merge range + // Skip cells within merged range + $excelCellIndex = Helper::colLetter($colIndex).$rowIndex; while ($this->isMerged($sheet, $excelCellIndex)) { $colIndex++; $excelCellIndex = Helper::colLetter($colIndex).$rowIndex; $sheet->cell($excelCellIndex); } + // Write cell $cellStyles = $this->getStyles($cell); $sheet->writeCell( trim($cell->getValue()), @@ -188,21 +189,16 @@ private function createExcel(): void $sheet->addNote(Excel::cellAddress($rowIndex, $colIndex), $cellComment); } + // Merge cells, if necessary $colspan = $cell->getAttribute('colspan'); $rowspan = $cell->getAttribute('rowspan'); if ($colspan || $rowspan) { - if ($colspan) { - $colspan = $colspan - 1; - } - - if ($rowspan) { - $rowspan = $rowspan - 1; - } + $colspan = $colspan ? $colspan - 1 : 0; + $rowspan = $rowspan ? $rowspan - 1 : 0; $mergeCellsTargetCellIndex = Helper::colLetter($colIndex + $colspan).($rowIndex + $rowspan); - - $sheet->mergeCells($excelCellIndex.':'.$mergeCellsTargetCellIndex); + $sheet->mergeCells($excelCellIndex . ':' . $mergeCellsTargetCellIndex); } $colIndex++;