使用 OpenXML SDK 1.0 将工作表与其名称关联
我正在使用 Microsoft 的 OpenXML SDK 1.0 版对 .xlsx 文件进行一些基本解析。我可以获取并解析工作表,并且可以获取工作表名称列表,但我无法弄清楚如何将哪个名称与哪个工作表链接起来。
据我所知,工作簿中的
这样的元素通过关系链接到特定工作表在 xl/_rels.xml
中定义,但我看不到 API 中公开的任何关系信息。
我使用的是 C#,但任何 VB.NET 示例都同样有帮助。
我觉得这应该很简单,但我无法弄清楚。看起来在 SDK v2.0 中可能会更简单,但目前还不能升级。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊……是的,它最终变得非常简单。
WorkbookPart
类公开了一个我一直在使用的WorksheetParts
属性,但它还公开了一个GetPartById(relationshipId)
方法。给定工作簿 XML 中的
元素列表(每个元素都包含名称和关系 id),我只需要通过 id 检索每个 WorksheetPart。Argh... yes, it ended up being dead simple. The
WorkbookPart
class exposes aWorksheetParts
property that I was hung up on using, but it also exposes aGetPartById(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.