0
Open XMLを使用してファイルを作成しようとしていますが、ヘッダーの最初の行を追加しようとするとファイルが壊れていて、開くことができません。ここで間違っていますか?オープンXMLを使用してExcelファイルを作成する
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();
}