iOfficeAI/OfficeCLI · critical · InvalidOperationException

Corrupt file: workbook missing

Error message

Corrupt file: workbook missing

What it means

Thrown by GetWorkbook when _doc.WorkbookPart or its .Workbook property is null. The package is missing xl/workbook.xml entirely — the file is structurally corrupt at the top level. InvalidOperationException (not ArgumentException) signals file-level corruption.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs:288

        bool needsReorder = false;
        for (int i = 0; i < children.Count; i++)
        {
            if (!ReferenceEquals(children[i], sorted[i]))
            {
                needsReorder = true;
                break;
            }
        }

        if (needsReorder)
        {
            foreach (var child in children) child.Remove();
            foreach (var child in sorted) ws.AppendChild(child);
        }
    }

    private Workbook GetWorkbook() =>
        _doc.WorkbookPart?.Workbook ?? throw new InvalidOperationException("Corrupt file: workbook missing");

    private List<(string Name, WorksheetPart Part)> GetWorksheets() => GetWorksheets(_doc);

    /// <summary>
    /// True when the workbook-level &lt;sheet&gt; element for the given name has
    /// State Hidden or VeryHidden. Used by the HTML preview to omit hidden sheets
    /// from the tab strip and content slider (matching real Excel).
    /// </summary>
    private bool IsSheetHidden(string sheetName)
    {
        var wbSheet = GetWorkbook().GetFirstChild<Sheets>()?.Elements<Sheet>()
            .FirstOrDefault(s => s.Name?.Value?.Equals(sheetName, StringComparison.OrdinalIgnoreCase) == true);
        return wbSheet?.State?.Value is { } st
            && (st == SheetStateValues.Hidden || st == SheetStateValues.VeryHidden);
    }

    private static List<(string Name, WorksheetPart Part)> GetWorksheets(SpreadsheetDocument doc)
    {

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Confirm the file is a real .xlsx (a valid OOXML zip with xl/workbook.xml).
  2. Open in Excel and let it repair, or regenerate from source.
  3. If the source is CSV/TSV, import it properly instead of renaming the extension.

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

static bool HasWorkbook(SpreadsheetDocument doc)
    => doc?.WorkbookPart?.Workbook != null;

Type guard

null

Try / catch

try { /* any workbook-level op */ }
catch (InvalidOperationException ex) when (ex.Message == "Corrupt file: workbook missing")
{ // file is not a valid .xlsx; refuse and ask for a valid one }

Prevention

When it happens

Trigger: Opening an .xlsx where xl/workbook.xml is absent, empty, or unreadable. Almost any workbook-level API (sheet list, named ranges, tables) hits GetWorkbook.

Common situations: A file that is actually a flat spreadsheet fragment (sheet XML only); zip corruption; a generator that wrote only sheet parts and skipped the workbook part; wrong file extension (e.g. .csv renamed .xlsx).

Related errors


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