Question

I'm using version 1.0 of Microsoft's OpenXML SDK to do some basic parsing of .xlsx files. I can get and parse the worksheets, and I can get a list of worksheet names, but I cannot for the life of me figure out how to link up what name goes with what worksheet.

I understand that an element like <sheet name="My Sheet" sheetId="1" r:id="rId1"/> in the workbook is linked up to a specific worksheet via the relationships defined in xl/_rels.xml, but I can't see where any of the relationship info is exposed in the API.

I'm using C#, but any VB.NET examples would be just as helpful.

I feel like this should be dead simple, but I can't figure it out. It also looks like it may be more straightforward in v2.0 of the SDK, but upgrading isn't an option at the moment.

Was it helpful?

Solution

Argh... yes, it ended up being dead simple. The WorkbookPart class exposes a WorksheetParts property that I was hung up on using, but it also exposes a GetPartById(relationshipId) method.

Given a list of <sheet/> elements from the workbook XML - each of which contains both a name and a relationship id - I just needed to retrieve each WorksheetPart by id.

OTHER TIPS

// Iterate Sheets; Get Name and xref WorksheetPart (container for Worksheet)
foreach (Sheet sheet in doc.WorkbookPart.Workbook.Sheets)
{
    string sName = sheet.Name;
    string sID = sheet.Id;

    WorksheetPart part = (WorksheetPart)doc.WorkbookPart.GetPartById(sID);
    Worksheet actualSheet = part.Worksheet;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top