Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Patch to avoid superfluous rightmost column #376

Open
@dennisverspuij

Description

@dennisverspuij

Sheets in Excel 2007 workbooks generated with PHPExcel might include an explicit additional empty rightmost column, visible in page break view. This may lead for example to an additional empty page upon printing. The superfluous column disappears again after saving the workbook again in Excel.

The following patch fixes this annoyance:

Index: PHPExcel/Writer/Excel2007/Worksheet.php
===================================================================
--- PHPExcel/Writer/Excel2007/Worksheet.php (revision 1811)
+++ PHPExcel/Writer/Excel2007/Worksheet.php (working copy)
@@ -965,9 +965,6 @@
            // sheetData
            $objWriter->startElement('sheetData');

-               // Get column count
-               $colCount = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn());
-
                // Highest row number
                $highestRow = $pSheet->getHighestRow();

@@ -995,7 +992,6 @@
                        // Start a new row
                        $objWriter->startElement('row');
                        $objWriter->writeAttribute('r', $currentRow);
-                       $objWriter->writeAttribute('spans', '1:' . $colCount);

                        // Row dimensions
                        if ($rowDimension->getRowHeight() >= 0) {
@@ -1026,7 +1022,21 @@

                        // Write cells
                        if (isset($cellsByRow[$currentRow])) {
+                           $min = 16385;
+                           $max = 0;
                            foreach($cellsByRow[$currentRow] as $cellAddress) {
+                               list($colIndex) = PHPExcel_Cell::coordinateFromString($cellAddress);
+                               $colIndex = PHPExcel_Cell::columnIndexFromString($colIndex);
+                               if ($colIndex < $min) {
+                                   $min = $colIndex;
+                               }
+                               if ($colIndex > $max) {
+                                   $max = $colIndex;
+                               }
+                           }
+                           $objWriter->writeAttribute('spans', $min.':'.$max);
+
+                           foreach($cellsByRow[$currentRow] as $cellAddress) {
                                // Write cell
                                $this->_writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable);
                            }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions