6

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.

4 Answers 4

6

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!

3
worksheet.WorksheetPart.GetParentParts().First() 

This should get the WorkBookPart, where worksheet is the WorkSheet instance.

2

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;
0

Which property are you looking for?

You can find the list of the properties on this page, especially, you can find the Workbook property, for instance, you would use DocumentFormat.OpenXml.Spreadsheet.Workbook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.