none
WorksheetParts.First() not working RRS feed

  • Question

  • I am just getting started feeling my way through an export to Excel using Open XML.  At this point, I am just trying to open my ‘results’ excel file and set up the ‘parts’ and see if I can ‘export’ the copied file from my web page without trying to add any rows of data yet.  The variable 'results' contains the pathname of an excel file I have copied from a template.  The code, up through the line getting the error, is:

    //Open the saved excel file that was created using template file.           

    using(SpreadsheetDocumentmyDoc = SpreadsheetDocument.Open(resultsFile, true))

    {

    // Create reference of main Workbook part.   

      WorkbookPartobjworkbook = myDoc.WorkbookPart;                

    // pick up first worksheet 

    WorksheetPartobjworksheet = objworkbook.WorksheetParts.First();

    Visual Studio is underlining 'First' in red, and the error message says:

    ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.WorksheetPart’ does not contain a definition for ‘First’ and no extension method ‘First’ accepting a first argument of type ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.’ could be found (are you missing a using directive or an assembly reference?)

    I DO have the reference ‘DocumentFormat.XML’ included in the solution, and I DO have the additional ‘using’ references at the top of the c# file:

    using DocumentFormat.OpenXml;

    using DocumentFormat.OpenXml.Packaging;

    using DocumentFormat.OpenXml.Spreadsheet;

    Also, visual studio does appear to be recognizing other parts of the syntax, like SpreadsheetDocument and WorkbookPart, etc.

    What am I missing?  I'd like to get the 'First' working; but what I'd really like to know how to do is reference the worksheet by name, as my file has two worksheets.  I found an example of code purporting to do this, but Visual Studio did not recognize that syntax either.

    Thanks for your help.

    Wednesday, December 23, 2015 7:17 PM

Answers

  • >>>Visual Studio is underlining 'First' in red, and the error message says:

    ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.WorksheetPart’ does not contain a definition for ‘First’ and no extension method ‘First’ accepting a first argument of type ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.’ could be found (are you missing a using directive or an assembly reference?)<<<

    According to your description, I have reproduced this issue, this issue is cause by that First() is a Linq extension method, you missed using System.Linq namespace.

    >>>but what I'd really like to know how to do is reference the worksheet by name, as my file has two worksheets.

    You could refer to below code:

    Sheet firstSheet = objworkbook.Workbook.Descendants<Sheet>().First(s => s.Name == "Sheet1");
    Worksheet firstWorksheet = ((WorksheetPart)objworkbook.GetPartById(firstSheet.Id)).Worksheet;

    • Proposed as answer by David_JunFeng Friday, December 25, 2015 1:02 AM
    • Marked as answer by David_JunFeng Tuesday, January 5, 2016 1:35 AM
    Thursday, December 24, 2015 5:27 AM

All replies

  • >>>Visual Studio is underlining 'First' in red, and the error message says:

    ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.WorksheetPart’ does not contain a definition for ‘First’ and no extension method ‘First’ accepting a first argument of type ‘System.Collections.Generic.IEnumerable<DocumentFormat.OpenXml.Packaging.’ could be found (are you missing a using directive or an assembly reference?)<<<

    According to your description, I have reproduced this issue, this issue is cause by that First() is a Linq extension method, you missed using System.Linq namespace.

    >>>but what I'd really like to know how to do is reference the worksheet by name, as my file has two worksheets.

    You could refer to below code:

    Sheet firstSheet = objworkbook.Workbook.Descendants<Sheet>().First(s => s.Name == "Sheet1");
    Worksheet firstWorksheet = ((WorksheetPart)objworkbook.GetPartById(firstSheet.Id)).Worksheet;

    • Proposed as answer by David_JunFeng Friday, December 25, 2015 1:02 AM
    • Marked as answer by David_JunFeng Tuesday, January 5, 2016 1:35 AM
    Thursday, December 24, 2015 5:27 AM
  • Thank you for your reply and for providing this alternate syntax, David. I appreciate your taking the time to look at this. Unfortunately, Visual Studio is not recognizing the 'First' in this syntax,either. I am now working on a 'theory' that the problem might be in our solution\Project's 'System' reference being too early a version - it is pointing at v2.0.50727\System.dll. Perhaps 'First' syntax was added in a later version? The visual studio version I'm working with is v4.5.50938; the configuration assemblies included in the Web.Config file are 'mostly' 3.5.00, but the configuration assemblies for System.Design and System.Windows.Forms are both pointing at v2.0.0.0. I don't know the reason behind the different versions, and I don't really know how all this stuff fits together. Do you think I might be on the right track with the version hypothesis? Thanks, Charlotte
    Wednesday, December 30, 2015 7:20 PM
  • Hi, CFischerInColorado

    System.Linq was introduced on .NET Framework 3.5, it is possible to use Linq in .NET 2.0 and 3.0 (and extension methods in general). I beleve the only requirement is the compiler be at least 3.0.
     
    This is the forum to discuss questions and feedback for Open XML Format SDK, but you issue is related to Visual Studio, so I suggest that you could post this issue to the MSDN forum for Visual Studio

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=visualstudio

    For more information, click here to refer about System.Linq Namespace

    Thanks for your understanding.

    Thursday, December 31, 2015 9:42 AM