简单的Excel创建不会打开文件已损坏

时间:2018-07-27 11:43:19

标签: c# error-handling openxml openxml-sdk

我正在尝试使用打开的xml创建具有多个工作表的简单excel文件,不幸的是,该文件在创建后无法打开。

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

  

我们发现了一个问题,您是否想尽可能地恢复?

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>

1 个答案:

答案 0 :(得分:2)

您看到了吗? 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();
        }