iOfficeAI/OfficeCLI · error · ArgumentException

Cell value{where} exceeds Excel's {MaxCellTextLength}-charac

Error message

Cell value{where} exceeds Excel's {MaxCellTextLength}-character limit (got {value.Length})

What it means

Excel cells hold at most 32767 (2^15 - 1) characters. A longer value causes Excel to fail open/save with 0x800A03EC and, worse, can leave the sheetData empty on disk (total data loss). The library rejects over-length values at write time via EnsureCellValueLength and also validates XML-illegal control chars/lone surrogates.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs:560

                 System.Text.RegularExpressions.Regex.Matches(scan, @"(?<![A-Za-z0-9_'.#])([A-Za-z_][A-Za-z0-9_.]*)!"))
        {
            if (!names.Contains(m.Groups[1].Value)) return true;
        }
        return false;
    }

    // R13-1: Excel rejects cell values longer than 32767 chars (2^15 - 1) with
    // 0x800A03EC on save/open. Reject at write time with a clear error rather
    // than silently writing a file Excel will refuse to open.
    internal const int MaxCellTextLength = 32767;

    internal static void EnsureCellValueLength(string? value, string? cellRef = null)
    {
        if (value == null) return;
        if (value.Length > MaxCellTextLength)
        {
            var where = string.IsNullOrEmpty(cellRef) ? "" : $" at {cellRef}";
            throw new ArgumentException(
                $"Cell value{where} exceeds Excel's {MaxCellTextLength}-character limit (got {value.Length})");
        }
        // XML-illegal control chars / lone surrogates: without this the value
        // enters the in-memory DOM fine and only fails at close-time save —
        // "save failed during shutdown", leaving sheetData empty on disk
        // (total data loss). Same guard Word text paths already use.
        OfficeCli.Core.ParseHelpers.ValidateXmlText(value,
            string.IsNullOrEmpty(cellRef) ? "cell value" : $"cell value at {cellRef}");
    }

    // Numeric literal forms Excel's <v> parser accepts. double.TryParse is
    // far more lenient (leading '+', thousands separators, padding, currency
    // NumberStyles) — writing such text verbatim into an untyped (numeric)
    // <v> makes real Excel refuse the file (0x800A03EC) even though
    // schema validation stays green.
    private static readonly System.Text.RegularExpressions.Regex CanonicalNumericLiteral =
        new(@"^-?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$", System.Text.RegularExpressions.RegexOptions.Compiled);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Truncate the value to 32767 chars before writing.
  2. Store oversized content in a sidecar text file and reference it, or split across multiple cells.
  3. Validate length upstream and surface the limit to the user.

Example fix

// before
sheet.SetValue("A1", giantJsonBlob);   // 50000 chars

// after
var v = giantJsonBlob.Length > 32767 ? giantJsonBlob.Substring(0, 32767) : giantJsonBlob;
sheet.SetValue("A1", v);
Defensive patterns

Strategy: validation

Validate before calling

const int MaxCellTextLength = 32767;
static string ClampCellValue(string? v) =>
    v == null ? string.Empty : (v.Length > MaxCellTextLength ? v.Substring(0, MaxCellTextLength) : v);

Try / catch

try { sheet.SetValue(cell, value); }
catch (ArgumentException ex) when (ex.Message.Contains("32767-character limit")) {
    sheet.SetValue(cell, value.Substring(0, 32767));
}

Prevention

When it happens

Trigger: Writing a string longer than 32767 chars to any cell via a set/value API. The MaxCellTextLength constant (32767) is the ceiling.

Common situations: Dumping logs, JSON blobs, stack traces, or full document text into a cell; concatenating many records into one cell; CSV import of an unbounded text field.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/38bfe5a8868b0089. Report an issue: GitHub.