从流中打开电子表格文档(Open XML SDK)

时间:2017-10-10 12:13:40

标签: c# excel openxml

我正在关注如何使用OpenXML SDK的以下文章: https://msdn.microsoft.com/en-us/library/office/ff478410.aspx

我有以下代码:

// Open a SpreadsheetDocument based on a stream.
SpreadsheetDocument spreadsheetDocument 
    = SpreadsheetDocument.Open(stream, true);
// Add a new worksheet.
WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();

Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart);

// 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 sheetName = "Sheet" + sheetId;

// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
spreadsheetDocument.WorkbookPart.Workbook.Save();

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

但我一直收到以下错误:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1729  'Worksheet' does not contain a constructor that takes 1 arguments   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 139 Active
Error   CS0246  The type or namespace name 'SheetData' could not be found (are you missing a using directive or an assembly reference?) MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 139 Active
Error   CS1061  'WorksheetPart' does not contain a definition for 'Worksheet' and no extension method 'Worksheet' accepting a first argument of type 'WorksheetPart' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 140 Active
Error   CS1061  'WorkbookPart' does not contain a definition for 'Workbook' and no extension method 'Workbook' accepting a first argument of type 'WorkbookPart' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 142 Active
Error   CS1061  'Sheets' does not contain a definition for 'Elements' and no extension method 'Elements' accepting a first argument of type 'Sheets' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 147 Active
Error   CS0246  The type or namespace name 'Sheet' could not be found (are you missing a using directive or an assembly reference?) MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 147 Active
Error   CS1061  'Sheets' does not contain a definition for 'Elements' and no extension method 'Elements' accepting a first argument of type 'Sheets' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 149 Active
Error   CS0246  The type or namespace name 'Sheet' could not be found (are you missing a using directive or an assembly reference?) MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 149 Active
Error   CS0246  The type or namespace name 'Sheet' could not be found (are you missing a using directive or an assembly reference?) MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 156 Active
Error   CS0246  The type or namespace name 'Sheet' could not be found (are you missing a using directive or an assembly reference?) MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 156 Active
Error   CS1061  'Sheets' does not contain a definition for 'Append' and no extension method 'Append' accepting a first argument of type 'Sheets' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 157 Active
Error   CS1061  'WorkbookPart' does not contain a definition for 'Workbook' and no extension method 'Workbook' accepting a first argument of type 'WorkbookPart' could be found (are you missing a using directive or an assembly reference?)   MyProject   C:\Users\brkar1\Documents\Visual Studio 2015\Projects\MyProject\MyProject\Controllers\HomeController.cs 158 Active

为什么会收到此错误消息?

1 个答案:

答案 0 :(得分:1)

确保您引用了添加到项目中的DocumentFormat.OpenXml.dll。