简单 excel 创建无法打开文件已损坏

Simple excel creation does not open file is corrupt

我正在尝试使用 open xml 创建一个包含多张纸的简单 excel 文件,不幸的是,该文件在创建后无法打开。

文件生成后,当我用 Microsoft Excel 打开它时,它显示

We found a problem, do you want to recover as much as we can?

using (SpreadsheetDocument spreedDoc = SpreadsheetDocument.Create(filePath,
    DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
    WorkbookPart wbPart = spreedDoc.WorkbookPart;

    wbPart = spreedDoc.AddWorkbookPart();
    wbPart.Workbook = new Workbook();

    Sheets sheets = wbPart.Workbook.AppendChild(new Sheets());

    foreach (var sheetData in excelSheetData)
    {
        // Add a blank WorksheetPart.
        WorksheetPart worksheetPart = wbPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());

        string relationshipId = wbPart.GetIdOfPart(worksheetPart);

        // Get a unique ID for the new worksheet.
        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        // Give the new worksheet a name.
        string sheetNameToWrite = sheetName;
        if (string.IsNullOrWhiteSpace(sheetNameToWrite))
        {
            sheetNameToWrite = "Sheet"+sheetId;
        }
        // Append the new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.AppendChild(sheet);
    }

    //wbPart.Workbook.Sheets.AppendChild(sheet);
    wbPart.Workbook.Save();
}

在 excel 中尝试修复时给出以下消息

-<repairedRecords summary="Following is a list of repairs:">

<repairedRecord>Repaired Records: Worksheet properties from /xl/workbook.xml part (Workbook)</repairedRecord>

</repairedRecords>

</recoveryLog>

你看到了吗? http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

在 OpenXML 中创建具有多个工作表的功能性 excel 文件(对我有用)的必要步骤如下:

using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
        {
            spreadsheet.AddWorkbookPart();
            spreadsheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

            spreadsheet.WorkbookPart.Workbook.Append(new BookViews(new WorkbookView()));

            WorkbookStylesPart workbookStylesPart = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>("rIdStyles");
            Stylesheet stylesheet = new Stylesheet();
            workbookStylesPart.Stylesheet = stylesheet;
            workbookStylesPart.Stylesheet.Save();

            for (int worksheetNo = 1; worksheetNo < worksheetCountYouWantToCreate; worksheetNo++)
            {
                string workSheetID = "rId" + worksheetNo;
                string worksheetName = "worksheet" + worksheetNo;

                WorksheetPart newWorksheetPart = spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                newWorksheetPart.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // write data here
                // ...
                // ...

                newWorksheetPart.Worksheet.Save();

                if (worksheetNo == 1)
                    spreadsheet.WorkbookPart.Workbook.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheets());

                spreadsheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadsheet.WorkbookPart.GetIdOfPart(newWorksheetPart),
                    SheetId = (uint)worksheetNo,
                    Name = worksheetName
                });
            }
            spreadsheet.WorkbookPart.Workbook.Save();
        }