How can I get a WorkbookPart from a WorkSheet?

I am trying to create an Excel file using the OpenXML SDK. I have one situation to get a WorkBookPart from an instance of WorkSheet. How can i get it? Thanks.
Ant.

+4
source share
4 answers

I know this is an old question, but I thought that I would give the full correct answer to what Ant was asking. I came across this question when I was looking for the same answer. It is tested and works.

Let's say for some reason you have a Worksheet object called a sheet:

Worksheet worksheet = ((WorksheetPart)_spreadsheet.WorkbookPart.GetPartById("rId1")).Worksheet; 

Now, maybe later in my program I need to get part of the workbook for some reason:

 WorkbookPart workbookPart = (WorkbookPart) worksheet.WorksheetPart.GetParentParts().First(); 

What all!

+4
source

There is a path from a worksheet to a workbook through the Package object:

 Worksheet ws = someWorksheet; SpreadsheetDocument ssDoc = ws.WorksheetPart.OpenXmlPackage as SpreadsheetDocument; Workbook = ssDoc.WorkbookPart.Workbook; 
+2
source
 worksheet.WorksheetPart.GetParentParts().First() 

This should turn WorkBookPart to be WorkBookPart , where the worksheet is an instance of WorkSheet .

+2
source

What property are you looking for?

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

0
source

Source: https://habr.com/ru/post/1308750/


All Articles