Open XML SDK 2.0 to get access to excel 2010 worksheet by name

后端 未结 2 949
野趣味
野趣味 2021-02-05 08:57

I have an Excel 2010 spreadsheet that has 3 worksheets named Sheet1, Sheet2 and Sheet3.

I\'m trying to get a reference to a worksheet by name.

I\'m using the co

2条回答
  •  遇见更好的自我
    2021-02-05 09:37

    What you really want is the WorksheetPart which is what contains the SheetData that you are looking for. Grabbing the Sheets under the Workbook will only give you certain metadata about the worksheets. Here is an example on how to grab that WorksheetPart (feel free to add error checking as you see fit as I assume the sheetName already exists by calling First and not FirstOrDefault)

    public WorksheetPart GetWorksheetPart(WorkbookPart workbookPart, string sheetName)
    {
        string relId = workbookPart.Workbook.Descendants().First(s => sheetName.Equals(s.Name)).Id;
        return (WorksheetPart)workbookPart.GetPartById(relId);
    }
    

    Then just use your code above to grab the correct SheetData reference and you will be able to find the data you want from there.

提交回复
热议问题