Ant
Ant

Reputation: 3459

How can I get WorkbookPart from WorkSheet?

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.

Upvotes: 6

Views: 4924

Answers (4)

cakiran
cakiran

Reputation: 341

worksheet.WorksheetPart.GetParentParts().First() 

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

Upvotes: 3

Justin
Justin

Reputation: 627

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!

Upvotes: 7

saarp
saarp

Reputation: 1951

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;

Upvotes: 2

JMax
JMax

Reputation: 26591

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

Upvotes: 0

Related Questions