使用打开 XML 创建 excel 文件
Using open XML to create excel file
我正在尝试使用 Open XML 创建一个文件,但是当试图只添加 headers 的第一行时,文件已损坏,我无法打开,谁能告诉我我在这里做错了什么?
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();
}
你这里有一些问题。
首先,您要将 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) 添加引用。鉴于单元格在您的代码中被称为 "headers" 我猜您实际上想要单元格 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();
}
我正在尝试使用 Open XML 创建一个文件,但是当试图只添加 headers 的第一行时,文件已损坏,我无法打开,谁能告诉我我在这里做错了什么?
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();
}
你这里有一些问题。
首先,您要将 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) 添加引用。鉴于单元格在您的代码中被称为 "headers" 我猜您实际上想要单元格 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();
}