使用 Open XML SDK 2.0 将超链接添加到 Excel 2007 中的单元格
我似乎找不到任何有关如何使用 Open XML SDK 2.0 将超链接添加到 Excel 2007 中的单元格的文档或代码示例。我正在使用以下代码,但是我缺少一个步骤吗?
WorksheetPart workSheetPart = ExcelUtilities.GetWorkSheetPart(mWorkBookPart, "Program");
workSheetPart.AddHyperlinkRelationship(new Uri("http://www.google.com", UriKind.Absolute), true);
workSheetPart.Worksheet.Save();
mWorkBookPart.Workbook.Save();
然后,当我尝试打开 Excel 文档时,它说文件已损坏,因为找不到超链接的关系 ID。如何设置或创建该关系 ID?
I can't seem to find any documentation or code samples on how to add a hyperlink to a cell in Excel 2007 using the Open XML SDK 2.0. I am using the following code, but is there a step I am missing?
WorksheetPart workSheetPart = ExcelUtilities.GetWorkSheetPart(mWorkBookPart, "Program");
workSheetPart.AddHyperlinkRelationship(new Uri("http://www.google.com", UriKind.Absolute), true);
workSheetPart.Worksheet.Save();
mWorkBookPart.Workbook.Save();
Then when I try and open the Excel document it says the file is corrupted because the Relationship Id for the hyperlink cannot be found. How do you setup or create that Relationship Id?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一种可能性(我使用过)是使用 HYPERLINK< /a> Excel 公式。我需要在每个单元格中创建单独的超链接,但单元格必须显示不同的文本(我必须在单元格中显示跟踪号码,但每个跟踪号码都有一个到运营商站点的超链接,并且必须处理多个运营商)。
一旦我实例化了一个单独的单元格,公式就会以这种方式应用于每个单元格(无疑有多种方式):
通过这种方式,我能够为每个单元格创建单独的超链接和文本。顺便说一句,链接将以默认字体颜色显示,除非您引用蓝色字体的样式。
希望这有帮助。
Another possibility, (which I used), is to use the HYPERLINK formula for Excel. I needed to create individual hyperlinks in each cell, yet the cells had to display different text, (I had to display tracking numbers in the cells yet have a hyperlink for each tracking number to the carrier's site and had to handle multiple carriers).
Once I instantiated an individual cell, the formula was applied in this manner to each cell (there are undoubtedly numerous way):
In this way, I was able to create individual hyperlinks and text for each cell. By the way, the links will appear with the default font color unless you reference a style with blue font.
Hope this helps.
我能够使用 System.IO.Packaging 代码向单元格添加超链接:
然后我使用 Open XML SDK 2.0 转换此代码,但它不起作用。看来
AddHyperlinkRelationship
方法实际上并未将关系添加到 .rels 文件中。我不知道为什么,但对我来说这确实是一个错误。I was able to add a hyperlink to a cell using
System.IO.Packaging
code:I then translated this code using the Open XML SDK 2.0 and it doesn't work. It seems the
AddHyperlinkRelationship
method doesn't actually add the relationship to the .rels file. I'm not sure why, but it sure seems like a bug to me.您应该将其添加到接受超链接的对象(例如单元格)而不是工作表中。像这样的东西应该适合你:
You should be adding it to an object that accepts hyperlinks, such as a cell, instead of the worksheet. Something like this should work for you:
最简单的方法是使用超链接公式,但默认情况下链接不是蓝色的,这就是设置 styleIndex 的原因。
将样式添加到工作簿:
http://www.dispatchertimer.com/tutorial/how-to-create-an-excel-file-in-net-using-openxml-part-3-add-stylesheet-to -电子表格/
Easiest way is to use the HyperLink formular, but the links aren't blue by default, this is why the styleIndex is set.
Adding the styling to the workbook:
http://www.dispatchertimer.com/tutorial/how-to-create-an-excel-file-in-net-using-openxml-part-3-add-stylesheet-to-the-spreadsheet/
我设法用这个函数创建一个超链接
I managed to create a hyperlink with this function