使用开放式XML创建Excel文件

9

我正在尝试使用Open XML创建一个文件,但是当我试图只添加第一行标题时,文件被损坏了,我无法打开,有人能告诉我我在这里做错了什么吗?

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("C:\\testpdfs\\mytest.xlsx", SpreadsheetDocumentType.Workbook))
{
    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // 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 = ViewBag.Title
    };

    Row row = new Row() { RowIndex = 1 };
    Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp") };
    row.Append(header1);
    Cell header2 = new Cell() { CellReference = "A2", CellValue = new CellValue("Settlement Interval") };
    row.Append(header2);
    Cell header3 = new Cell() { CellReference = "A3", CellValue = new CellValue("Aggregated Consumption Factor") };
    row.Append(header3);
    Cell header4 = new Cell() { CellReference = "A4", CellValue = new CellValue("Loss Adjusted Aggregated Consumption") };
    row.Append(header4);

    sheet.Append(row);
    sheets.Append(sheet);
    //sheet.Append(row);
    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
    return View();
}
1个回答

20

您在这里有几个问题。

首先,您正在将row添加到Sheet,但需要将其添加到SheetData。最简单的方法是保留对SheetData对象的引用,以便我们稍后可以使用它:

SheetData sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
...
sheetData.Append(row);

其次,您需要明确地为每个单元格指定数据类型,因为如果没有指定数据类型,则默认数据类型为数字:

Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp"), DataType = CellValues.String };

最后,你的单元格引用不太正确。你把所有东西都添加到了一行中,但是却添加了对第1至4行(A1-A4)的引用。鉴于在你的代码中这些单元格被称为“标题”,我猜想你实际上想要的是单元格A1-D1中的值,因此你只需要更新CellReference的值即可。如果你实际上想要A1-A4中的值,那么你需要将每个单元格添加到一个新行中。

完整的代码列表(假设你想要单元格A1-D1)如下:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("C:\\testpdfs\\mytest.xlsx", SpreadsheetDocumentType.Workbook))
{
    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    SheetData sheetData = new SheetData();
    worksheetPart.Worksheet = new Worksheet(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 = ViewBag.Title
    };

    Row row = new Row() { RowIndex = 1 };
    Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp"), DataType = CellValues.String };
    row.Append(header1);
    Cell header2 = new Cell() { CellReference = "B1", CellValue = new CellValue("Settlement Interval"), DataType = CellValues.String };
    row.Append(header2);
    Cell header3 = new Cell() { CellReference = "C1", CellValue = new CellValue("Aggregated Consumption Factor"), DataType = CellValues.String };
    row.Append(header3);
    Cell header4 = new Cell() { CellReference = "D1", CellValue = new CellValue("Loss Adjusted Aggregated Consumption"), DataType = CellValues.String };
    row.Append(header4);

    sheetData.Append(row);

    sheets.Append(sheet);

    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
    return View();

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接