Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get WorkbookPart from WorkSheet?

Tags:

excel

openxml

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.

like image 942
Ant Avatar asked May 04 '10 09:05

Ant


3 Answers

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!

like image 128
Justin Avatar answered Oct 19 '22 23:10

Justin


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;
like image 32
saarp Avatar answered Oct 19 '22 22:10

saarp


worksheet.WorksheetPart.GetParentParts().First() 

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

like image 34
cakiran Avatar answered Oct 20 '22 00:10

cakiran