How can I get WorkbookPart from WorkSheet?
I am trying to 开发者_JAVA技巧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!
worksheet.WorksheetPart.GetParentParts().First()
This should get the WorkBookPart
, where worksheet is the WorkSheet
instance.
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;
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
精彩评论