{"record":{"id":"af9fe2c3a6a633bd","repo":"iOfficeAI/OfficeCLI","slug":"corrupt-file-workbook-missing","errorCode":null,"errorMessage":"Corrupt file: workbook missing","messagePattern":"Corrupt file: workbook missing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs","lineNumber":288,"sourceCode":"        bool needsReorder = false;\n        for (int i = 0; i < children.Count; i++)\n        {\n            if (!ReferenceEquals(children[i], sorted[i]))\n            {\n                needsReorder = true;\n                break;\n            }\n        }\n\n        if (needsReorder)\n        {\n            foreach (var child in children) child.Remove();\n            foreach (var child in sorted) ws.AppendChild(child);\n        }\n    }\n\n    private Workbook GetWorkbook() =>\n        _doc.WorkbookPart?.Workbook ?? throw new InvalidOperationException(\"Corrupt file: workbook missing\");\n\n    private List<(string Name, WorksheetPart Part)> GetWorksheets() => GetWorksheets(_doc);\n\n    /// <summary>\n    /// True when the workbook-level &lt;sheet&gt; element for the given name has\n    /// State Hidden or VeryHidden. Used by the HTML preview to omit hidden sheets\n    /// from the tab strip and content slider (matching real Excel).\n    /// </summary>\n    private bool IsSheetHidden(string sheetName)\n    {\n        var wbSheet = GetWorkbook().GetFirstChild<Sheets>()?.Elements<Sheet>()\n            .FirstOrDefault(s => s.Name?.Value?.Equals(sheetName, StringComparison.OrdinalIgnoreCase) == true);\n        return wbSheet?.State?.Value is { } st\n            && (st == SheetStateValues.Hidden || st == SheetStateValues.VeryHidden);\n    }\n\n    private static List<(string Name, WorksheetPart Part)> GetWorksheets(SpreadsheetDocument doc)\n    {","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs#L270-L306","documentation":"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.","triggerScenarios":"Opening an .xlsx where xl/workbook.xml is absent, empty, or unreadable. Almost any workbook-level API (sheet list, named ranges, tables) hits GetWorkbook.","commonSituations":"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).","solutions":["Confirm the file is a real .xlsx (a valid OOXML zip with xl/workbook.xml).","Open in Excel and let it repair, or regenerate from source.","If the source is CSV/TSV, import it properly instead of renaming the extension."],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"static bool HasWorkbook(SpreadsheetDocument doc)\n    => doc?.WorkbookPart?.Workbook != null;","typeGuard":"null","tryCatchPattern":"try { /* any workbook-level op */ }\ncatch (InvalidOperationException ex) when (ex.Message == \"Corrupt file: workbook missing\")\n{ // file is not a valid .xlsx; refuse and ask for a valid one }","preventionTips":["Check the file is a genuine OOXML package before processing.","A renamed CSV is not an .xlsx — import instead of rename.","Catch InvalidOperationException for corruption vs ArgumentException for bad input."],"tags":["excel","corrupt-file","ooxml","workbook"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}