2017-10-10 6 views
0

Im suivant l'article suivant sur la façon de travailler avec le SDK OpenXML: https://msdn.microsoft.com/en-us/library/office/ff478410.aspxOuvrez un document de feuille de calcul à partir d'un flux (Open XML SDK)

je le code suivant:

// 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(); 

Mais je continuer à obtenir l'erreur suivante:

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 

Toute personne qui peut m'aider avec cela? Pourquoi ai-je ce message d'erreur?

+0

Avez-vous ajouté une référence à DocumentFormat.OpenXml.dll à votre projet? –

+0

@GabrielRainha: Oui – Bryan

+0

@GabrielRainha: Hm, j'avais une référence à Il dans mon projet, mais quand j'ai cherché DocumentFormat dans Nuget, Il a dit qu'il n'était pas installé. Je l'ai maintenant installé et les erreurs sont parties :) – Bryan

Répondre

1

Assurez-vous qu'une référence à DocumentFormat.OpenXml.dll a été ajoutée à votre projet.