【问题标题】:Open a spreadsheet document from a stream (Open XML SDK)从流中打开电子表格文档 (Open XML SDK)
【发布时间】:2017-10-10 12:13:39
【问题描述】:

我正在关注以下有关如何使用 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

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

【问题讨论】:

  • 您是否在项目中添加了对 DocumentFormat.OpenXml.dll 的引用?
  • @GabrielRainha:是的
  • @GabrielRainha: 嗯,我的项目中有提到它,但是当我在 Nuget 中搜索 DocumentFormat 时,它说它没有安装。我现在已经安装了它并且错误消失了:)
  • 好!要我发表评论以标记为答案吗?
  • @GabrielRainha:是的,ofc :)

标签: c# excel openxml


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-29
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多