{"record":{"id":"14105c25310211e0","repo":"iOfficeAI/OfficeCLI","slug":"import-exceeds-excel-s-row-limit-data-would-reach","errorCode":null,"errorMessage":"Import exceeds Excel's row limit: data would reach row {endRowReq} (maximum {ExcelMaxRow}). Reduce the CSV or change the start cell.","messagePattern":"Import exceeds Excel's row limit: data would reach row (.+?) \\(maximum (.+?)\\)\\. Reduce the CSV or change the start cell\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Import.cs","lineNumber":54,"sourceCode":"        var startColIdx = ColumnNameToIndex(startCol);\n\n        // Parse CSV\n        var rows = ParseCsv(csvContent, delimiter);\n        if (rows.Count == 0)\n            return \"No data to import\";\n\n        int maxCols = 0;\n        for (int r = 0; r < rows.Count; r++)\n            if (rows[r].Count > maxCols) maxCols = rows[r].Count;\n\n        // DOS-hardening: reject imports that exceed Excel's sheet dimensions\n        // BEFORE writing anything. Without this an over-sized CSV (e.g. >XFD\n        // columns or >1048576 rows) spun indefinitely instead of erroring.\n        const int ExcelMaxRow = 1048576;\n        const int ExcelMaxCol = 16384; // XFD (ColumnNameToIndex is 1-based)\n        long endRowReq = (long)startRow + rows.Count - 1;\n        if (endRowReq > ExcelMaxRow)\n            throw new ArgumentException(\n                $\"Import exceeds Excel's row limit: data would reach row {endRowReq} \" +\n                $\"(maximum {ExcelMaxRow}). Reduce the CSV or change the start cell.\");\n        long endColIdx = (long)startColIdx + maxCols - 1;\n        if (endColIdx > ExcelMaxCol)\n            throw new ArgumentException(\n                $\"Import exceeds Excel's column limit: data would reach column {endColIdx} \" +\n                $\"(maximum {ExcelMaxCol} / XFD). Reduce the CSV width or change the start cell.\");\n\n        // BUG-R11-import-dup-row BUG-11: import previously always appended a\n        // brand-new <row r=\"N\">, producing duplicate row entries when the\n        // target rows already existed (Excel auto-repaired by keeping the\n        // first one, silently losing imported data). Upsert by RowIndex —\n        // reuse an existing row, otherwise insert a new one in sorted position.\n        //\n        // PERF(dos-hardening): the previous implementation re-scanned the whole\n        // SheetData (LINQ FirstOrDefault) for every imported row AND every cell,\n        // making a bulk import O(rows*cells * existing) — a 100k-row CSV took\n        // 9+ minutes. Pre-index existing rows once and walk them with an","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Import.cs#L36-L72","documentation":"Thrown by ExcelHandler.Import as a DOS-hardening guard BEFORE writing any cells, when startRow + rows.Count - 1 would exceed Excel's maximum of 1,048,576 rows. Without this check an oversized import spun indefinitely instead of erroring.","triggerScenarios":"Importing a CSV with more rows than Excel can hold from the given startRow, e.g. startCell=A1 with a 1,100,000-row CSV, or startCell=A100000 with a 1,000,000-row CSV (the offset pushes the end past the limit).","commonSituations":"Bulk-ingesting a database extract or log without checking row count; mis-estimating CSV size; choosing a startCell deep in the sheet (e.g. A500000) and then importing a large payload.","solutions":["Reduce the CSV to at most (1048576 - startRow + 1) rows before importing.","Move the startCell earlier (A1) to maximize available rows.","Split the data across multiple sheets or filter/aggregate the source before import."],"exampleFix":"// before\nvar csv = File.ReadAllText(\"huge.csv\");           // 1.1M rows\nexcel.Import(\"/Sheet1\", csv, ',', false, \"A1\");\n\n// after (trim to the sheet capacity)\nvar rows = File.ReadAllLines(\"huge.csv\").Take(1_048_576);\nvar csv  = string.Join('\\n', rows);\nexcel.Import(\"/Sheet1\", csv, ',', false, \"A1\");","handlingStrategy":"validation","validationCode":"const int ExcelMaxRow = 1048576;\nvar (startCol, startRow) = ExcelHandler.ParseCellReference(startCell.ToUpperInvariant());\nlong endRow = (long)startRow + rowCount - 1;\nif (endRow > ExcelMaxRow)\n    throw new ArgumentException($\"Import would reach row {endRow} (max {ExcelMaxRow}).\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Count CSV rows before importing and reject or page payloads over the limit.","Start at A1 to use the full row budget.","Split very large datasets across multiple sheets."],"tags":["excel","import","validation","dos-hardening","bounds"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}