{"record":{"id":"ed4e93bf806b443c","repo":"PHPOffice/PhpSpreadsheet","slug":"rows-or-columns-overflow-excel5-has-limit-to-6553","errorCode":null,"errorMessage":"Rows or columns overflow! Excel5 has limit to 65535 rows and 255 columns. Use XLSX instead.","messagePattern":"Rows or columns overflow! Excel5 has limit to 65535 rows and 255 columns\\. Use XLSX instead\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/Xls/Worksheet.php","lineNumber":364,"sourceCode":"            $this->writeRow(\n                $rowDimension->getRowIndex() - 1,\n                (int) $rowDimension->getRowHeight(),\n                $xfIndex,\n                !$rowDimension->getVisible(),\n                $rowDimension->getOutlineLevel()\n            );\n        }\n\n        // Write Cells\n        foreach ($phpSheet->getCellCollection()->getSortedCoordinates() as $coordinate) {\n            /** @var Cell $cell */\n            $cell = $phpSheet->getCellCollection()->get($coordinate);\n            $row = $cell->getRow() - 1;\n            $column = Coordinate::columnIndexFromString($cell->getColumn()) - 1;\n\n            // Don't break Excel break the code!\n            if ($row > 65535 || $column > 255) {\n                throw new WriterException('Rows or columns overflow! Excel5 has limit to 65535 rows and 255 columns. Use XLSX instead.');\n            }\n\n            // Write cell value\n            $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs\n\n            $cVal = $cell->getValue();\n            if ($cVal instanceof RichText && (string) $cVal === '') {\n                $cVal = '';\n            }\n            if ($cVal instanceof RichText) {\n                $arrcRun = [];\n                $str_pos = 0;\n                $elements = $cVal->getRichTextElements();\n                foreach ($elements as $element) {\n                    // FONT Index\n                    $str_fontidx = 0;\n                    if ($element instanceof Run) {\n                        $getFont = $element->getFont();","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/Xls/Worksheet.php#L346-L382","documentation":"The legacy Xls (BIFF8) binary format supports at most 65,536 rows and 256 columns per worksheet. During save, the Xls writer iterates the sheet's sorted cell coordinates and throws as soon as one cell has a row number above 65536 or a column beyond 'IV' (the checks are 0-based: row index > 65535, column index > 255). Only cells that actually exist are checked - empty rows/columns beyond the data never trigger it.","triggerScenarios":"Any cell at row 65537+ or column 'IW'+ in any worksheet, then ->save('out.xls'); typically after importing a large Xlsx/CSV (Xlsx allows 1,048,576 rows) or generating a big report into a workbook exported as Xls.","commonSituations":"Migrating an export from Xlsx to Xls because a downstream legacy system requires .xls; large CSV imports saved back out as Xls; loop bugs (e.g. transposed row/column variables) writing stray cells at extreme coordinates.","solutions":["Use the Xlsx or Csv writer instead of Xls when data exceeds the limit (as the message itself suggests)","Split the dataset across multiple worksheets, each within 65,536 rows x 256 columns","Trim the data: remove out-of-range cells or apply a write filter before save","Fix runaway loops that accidentally write to coordinates outside the intended range"],"exampleFix":"// before\n$writer = new \\PhpOffice\\PhpSpreadsheet\\Writer\\Xls($spreadsheet);\n$writer->save('out.xls');\n\n// after\n$writer = new \\PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx($spreadsheet);\n$writer->save('out.xlsx');","handlingStrategy":"validation","validationCode":"const XLS_MAX_ROW = 65536, XLS_MAX_COL = 256; // BIFF8 limits\n\nforeach ($spreadsheet->getAllSheets() as $sheet) {\n    [$col, $row] = Coordinate::coordinateFromString($sheet->getHighestColumn() . '1');\n    $maxRow = (int) $sheet->getHighestRow();\n    $maxCol = Coordinate::columnIndexFromString($sheet->getHighestColumn());\n    if ($maxRow > XLS_MAX_ROW || $maxCol > XLS_MAX_COL) {\n        throw new RangeException(sprintf(\n            \"Sheet '%s' is %dx%d; Xls supports %dx%d. Use the Xlsx writer.\",\n            $sheet->getTitle(), $maxCol, $maxRow, XLS_MAX_COL, XLS_MAX_ROW\n        ));\n    }\n}\n\n$writer = new \\PhpOffice\\PhpSpreadsheet\\Writer\\Xls($spreadsheet);","typeGuard":null,"tryCatchPattern":"try {\n    $writer->save('out.xls');\n} catch (\\PhpOffice\\PhpSpreadsheet\\Writer\\Exception $e) {\n    if (str_contains($e->getMessage(), 'Rows or columns overflow')) {\n        $writer = new \\PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx($spreadsheet); // fallback format\n        $writer->save('out.xlsx');\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Check getHighestRow()/getHighestColumn() against 65536/'IV' before every Xls save","In import-export pipelines, prefer Xlsx or Csv outputs unless the consumer specifically demands .xls","Add an integration test that exports your largest realistic dataset through the Xls writer"],"tags":["phpspreadsheet","xls","limits","rows","columns","overflow"],"backgroundTag":"spreadsheet-row-limit-exceeded","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}