Fix BIFF8 DIMENSIONS record to use 0-based column indices

The XLS writer incorrectly used 1-based column indices in the BIFF8
DIMENSIONS record, violating the Microsoft Excel Binary File Format
specification which requires 0-based indices.

This bug caused an extra empty column to appear when converting XLS
files to other formats (e.g., CSV).

Changes:
- Modified column index initialization to subtract 1 from the result
  of Coordinate::columnIndexFromString() to convert from 1-based to
  0-based indexing
- Updated COLINFO loop to use the corrected 0-based lastColumnIndex

Per BIFF8 specification:
- colMic (first column) must be 0-based
- colMac (column after last column) must be 0-based

Example: For columns A-G (7 columns):
- Before: colMic=1, colMac=8 (incorrect)
- After:  colMic=0, colMac=7 (correct)

Fixes #4682
This commit is contained in:
Nebojša Zlatanović
2025-10-16 18:55:34 +02:00
parent eecfb6712d
commit 387ed72ca4
+5 -3
View File
@@ -214,8 +214,9 @@ class Worksheet extends BIFFwriter
$this->firstRowIndex = $minR;
$this->lastRowIndex = ($maxR > 65535) ? 65535 : $maxR;
$this->firstColumnIndex = Coordinate::columnIndexFromString($minC);
$this->lastColumnIndex = Coordinate::columnIndexFromString($maxC);
// BIFF8 requires 0-based column indices, but columnIndexFromString() returns 1-based
$this->firstColumnIndex = Coordinate::columnIndexFromString($minC) - 1;
$this->lastColumnIndex = Coordinate::columnIndexFromString($maxC) - 1;
if ($this->lastColumnIndex > 255) {
$this->lastColumnIndex = 255;
@@ -258,7 +259,8 @@ class Worksheet extends BIFFwriter
}
$columnDimensions = $phpSheet->getColumnDimensions();
$maxCol = $this->lastColumnIndex - 1;
// lastColumnIndex is now 0-based, so no need to subtract 1
$maxCol = $this->lastColumnIndex;
for ($i = 0; $i <= $maxCol; ++$i) {
$hidden = 0;
$level = 0;