How can I get the Worksheetpart from name or sheet ID in OpenXML?

倖福魔咒の 提交于 2019-12-04 06:31:14
Adrian

You're almost there. You're looping over doc.WorkbookPart.Workbook.Sheets already. All you should need to do after that is insert an if statement to see if the sheet you're looking for is your current point in the loop by looking at the properties s.Name or s.Id

Alternatively, as indicated here, you can use LINQ to select the worksheet by name or ID directly:

sID as Integer = doc.WorkbookPart.Workbook.Descendants(Sheet)().First(s => s.Name.Equals("First")).Id

Or

sID as Integer  = doc.WorkbookPart.Workbook.Descendants(Sheet)().First(s => s.Id.Equals(2)).Id

Once you have that ID you can do

wsp As WorksheetPart = doc.WorkbookPart.GetPartById(sID)

I apologise if there's bugs in this, I'm doing this on a rapidly-moving train using my brain compiler on an iPhone. Hopefully that should get you move in the right direction at least.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!