none
Problem to Append line RRS feed

  • Question

  • Hi,
    How to resolve
    Error	2	'System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Spreadsheet.Sheet>' does not contain a definition for 'Append' and no extension method 'Append' accepting a first argument of type 'System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Spreadsheet.Sheet>' could be found (are you missing a using directive or an assembly reference?)	C:\App0\App5\Program.cs	166	40	App5
    


    due to Append line below?
                                IEnumerable<Sheet> sheets = spreadSheet.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == vSN);
                                if (sheets.Count() == 0)
                                {
                                    // Add a WorkbookPart to the document.
                                    WorkbookPart workbookpart = spreadSheet.AddWorkbookPart();
                                    workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
    
                                    // Add a WorksheetPart to the WorkbookPart.
                                    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                                    worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());
    
                                    Sheet sheet = new Sheet()
                                    {
                                        Id = spreadSheet.WorkbookPart.
                                            GetIdOfPart(worksheetPart),
                                        SheetId = 1,
                                        Name = vSN
                                    };
                                    sheets.Append(sheet);
    
                                }
                                



    Many Thanks & Best Regards, Hua Min

    Wednesday, December 28, 2016 9:22 AM

Answers

  • Hi HuaMin  Chen,

    line mentioned below causing the error.

     IEnumerable<Sheet> sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == vSN);

    replace with line mentioned below.

      Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

    updated code :

     static void Main(string[] args)
            {
                string filepath = @"C:\Users\v-padee\Desktop\xmldemo.xlsx";
                SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
                string vSN = "mydemoSheet";
              
                    // Add a WorkbookPart to the document.
                    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                    workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
    
                    // Add a WorksheetPart to the WorkbookPart.
                    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                    worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());
                    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
                    Sheet sheet = new Sheet()
                    {
                        Id = spreadsheetDocument.WorkbookPart.
                            GetIdOfPart(worksheetPart),
                        SheetId = 1,
                        Name = vSN
                    };
                    sheets.Append(sheet);
                spreadsheetDocument.Close();
            }

    now it is working on my side after I made changes mentioned above.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jackson_1990 Thursday, December 29, 2016 2:50 AM
    Thursday, December 29, 2016 1:19 AM
    Moderator