Skip to content

Commit 387ed72

Browse files
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
1 parent eecfb67 commit 387ed72

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ public function __construct(int &$str_total, int &$str_unique, array &$str_table
214214
$this->firstRowIndex = $minR;
215215
$this->lastRowIndex = ($maxR > 65535) ? 65535 : $maxR;
216216

217-
$this->firstColumnIndex = Coordinate::columnIndexFromString($minC);
218-
$this->lastColumnIndex = Coordinate::columnIndexFromString($maxC);
217+
// BIFF8 requires 0-based column indices, but columnIndexFromString() returns 1-based
218+
$this->firstColumnIndex = Coordinate::columnIndexFromString($minC) - 1;
219+
$this->lastColumnIndex = Coordinate::columnIndexFromString($maxC) - 1;
219220

220221
if ($this->lastColumnIndex > 255) {
221222
$this->lastColumnIndex = 255;
@@ -258,7 +259,8 @@ public function close(): void
258259
}
259260

260261
$columnDimensions = $phpSheet->getColumnDimensions();
261-
$maxCol = $this->lastColumnIndex - 1;
262+
// lastColumnIndex is now 0-based, so no need to subtract 1
263+
$maxCol = $this->lastColumnIndex;
262264
for ($i = 0; $i <= $maxCol; ++$i) {
263265
$hidden = 0;
264266
$level = 0;

0 commit comments

Comments
 (0)