none
Problem to DocumentFormat RRS feed

  • Question

  • Hi,
    How to correct this
    Error	2	'DocumentFormat.OpenXml.Packaging.SpreadsheetDocument' does not contain a definition for 'WorksheetParts' and no extension method 'WorksheetParts' accepting a first argument of type 'DocumentFormat.OpenXml.Packaging.SpreadsheetDocument' could be found (are you missing a using directive or an assembly reference?)
    


    due to 2nd line below?

                        SpreadsheetDocument Book0 = SpreadsheetDocument.Open(openFileDialog1.FileName, false);
                        WorksheetPart worksheetPart = Book0.WorksheetParts.First();



    Many Thanks & Best Regards, Hua Min

    • Moved by CoolDadTx Tuesday, July 26, 2016 1:27 PM Office related
    Tuesday, July 26, 2016 8:46 AM

Answers

  • I think WorksheetsPart is part of a WorkbookPart and Workbook. Try something like the following:

    using (SpreadsheetDocument Book0 = SpreadsheetDocument.Open(docName, true))
    {
    	IEnumerable<Sheet> sheets = Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == worksheetName);
    	if (sheets.Count() == 0)
    	{
    		// The specified worksheet does not exist.
    		return;
    	}
    
    	WorksheetPart worksheetPart = (WorksheetPart)Book0.WorkbookPart.GetPartById(sheets.First().Id);
    	Worksheet worksheet = worksheetPart.Worksheet;
    	
    	
    	// Do something ....
    }
    
    wizend

    • Proposed as answer by David_JunFeng Friday, August 5, 2016 8:49 AM
    • Marked as answer by David_JunFeng Friday, August 5, 2016 9:35 AM
    Tuesday, July 26, 2016 11:20 AM
  • Hi HuaMin Chen,

    According to your description, I have reproduced this issue, I agree with Wizend's opinion.

    You could modify your code like below, but OpenXml WorksheetParts.First() is not always the 1st sheet MS Excel. 
    SpreadsheetDocument Book0 = SpreadsheetDocument.Open(openFileDialog1.FileName, false);
    WorksheetPart worksheetPart = Book0.WorkbookPart.WorksheetParts.First();

    For more information, click here to refer about WorkbookPart.WorksheetParts property


    • Marked as answer by David_JunFeng Friday, August 5, 2016 9:35 AM
    Wednesday, July 27, 2016 2:07 AM

All replies

  • I think WorksheetsPart is part of a WorkbookPart and Workbook. Try something like the following:

    using (SpreadsheetDocument Book0 = SpreadsheetDocument.Open(docName, true))
    {
    	IEnumerable<Sheet> sheets = Book0.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == worksheetName);
    	if (sheets.Count() == 0)
    	{
    		// The specified worksheet does not exist.
    		return;
    	}
    
    	WorksheetPart worksheetPart = (WorksheetPart)Book0.WorkbookPart.GetPartById(sheets.First().Id);
    	Worksheet worksheet = worksheetPart.Worksheet;
    	
    	
    	// Do something ....
    }
    
    wizend

    • Proposed as answer by David_JunFeng Friday, August 5, 2016 8:49 AM
    • Marked as answer by David_JunFeng Friday, August 5, 2016 9:35 AM
    Tuesday, July 26, 2016 11:20 AM
  • Hi HuaMin Chen,

    According to your description, I have reproduced this issue, I agree with Wizend's opinion.

    You could modify your code like below, but OpenXml WorksheetParts.First() is not always the 1st sheet MS Excel. 
    SpreadsheetDocument Book0 = SpreadsheetDocument.Open(openFileDialog1.FileName, false);
    WorksheetPart worksheetPart = Book0.WorkbookPart.WorksheetParts.First();

    For more information, click here to refer about WorkbookPart.WorksheetParts property


    • Marked as answer by David_JunFeng Friday, August 5, 2016 9:35 AM
    Wednesday, July 27, 2016 2:07 AM