I am trying to create Excel file using OpenXML SDK. I have one situation to get WorkBookPart from WorkSheet instance. How can I get it?
Thanks.
Ant.
I know this is an old question, but I thought I would give the full RIGHT answer of what Ant was asking. I came across this question when I was searching for the same answer. This is tested and works.
Lets say for some reason you have a Worksheet object named worksheet:
Worksheet worksheet = ((WorksheetPart)_spreadsheet.WorkbookPart.GetPartById("rId1")).Worksheet;
Now maybe, later on in my program I need to get the Workbook Part for some reason:
WorkbookPart workbookPart = (WorkbookPart) worksheet.WorksheetPart.GetParentParts().First();
That's all!
There is a path from Worksheet to Workbook through the Package object:
Worksheet ws = someWorksheet;
SpreadsheetDocument ssDoc = ws.WorksheetPart.OpenXmlPackage as SpreadsheetDocument;
Workbook = ssDoc.WorkbookPart.Workbook;
worksheet.WorksheetPart.GetParentParts().First()
This should get the WorkBookPart
, where worksheet is the WorkSheet
instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With