导出到Excel(OpenXML)会导致无法读取的内容

时间:2014-10-08 09:19:26

标签: c# excel openxml

我一直在为我的xlsx文件创建工作表。添加第一个工作表不是问题,但是当我想要第二个工作表时,导出的xlsx似乎已损坏。谁可以向我指出我做错了什么? 注意:我已经尝试过调用工作簿部分,Workbook.Save();'在创建第一个工作簿之后,但没有所需的结果。

protected void export_Click(object sender, EventArgs e)
{
    ExportToExcel(@"D:\dev\Dotnet4\Excel\test.xlsx");
}

private void ExportToExcel(string filepath)
{
    SpreadsheetDocument spreadsheetDocument;
    WorkbookPart workbookpart;
    CreateSpreadsheet(filepath, out spreadsheetDocument, out workbookpart);

    CreateWorksheet(spreadsheetDocument, workbookpart, "My sheet 1");
    CreateWorksheet(spreadsheetDocument, workbookpart, "My sheet 2");

    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
}

private static void CreateWorksheet(SpreadsheetDocument spreadsheetDocument, WorkbookPart workbookpart, string worksheetName)
{
    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    // Add Sheets to the Workbook.
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
        AppendChild<Sheets>(new Sheets());

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet()
    {
        Id = spreadsheetDocument.WorkbookPart.
            GetIdOfPart(worksheetPart),
        SheetId = 1,
        Name = worksheetName
    };
    sheets.Append(sheet);
}

private static void CreateSpreadsheet(string filepath, out SpreadsheetDocument spreadsheetDocument, out WorkbookPart workbookpart)
{
    // Create a spreadsheet document by supplying the filepath.
    // By default, AutoSave = true, Editable = true, and Type = xlsx.
    spreadsheetDocument = SpreadsheetDocument.
        Create(filepath, SpreadsheetDocumentType.Workbook);

    // Add a WorkbookPart to the document.
    workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();
}

1 个答案:

答案 0 :(得分:0)

我认为你有两个小问题:

  1. 对于您添加的两张无效的工作表,SheetId是相同的。
  2. 每次调用Sheets时,您都会添加一个新的CreateWorksheet元素,但XML中只应该有一个这样的元素(有很多Sheet但是没有很多Sheets!)。
  3. 要解决1,您可以使用sheets.ChildElements的计数。由于这是0第一次,第二次是你需要添加1。如果您愿意,可以将其作为参数带入CreateWorksheet;只要它们从1开始并且不同,它就不重要了。

    要解决2,您可以对Sheets的{​​{1}}属性执行空检查,只有在它不存在的情况下才会创建。

    以下内容应该是你所追求的。

    Workbook

    最后一点是private static void CreateWorksheet(SpreadsheetDocument spreadsheetDocument, WorkbookPart workbookpart, string worksheetName) { // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); // Add Sheets to the Workbook. if (spreadsheetDocument.WorkbookPart.Workbook.Sheets == null) { //spreadsheetDocument.WorkbookPart.Workbook.Sheets = new Sheets(); spreadsheetDocument.WorkbookPart.Workbook .AppendChild<Sheets>(new Sheets()); } Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets; // Append a new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart. GetIdOfPart(worksheetPart), SheetId = (UInt32)sheets.ChildElements.Count + 1, Name = worksheetName }; sheets.Append(sheet); } 实施SpreadsheetDocument所以它应该在使用后处理。