超链接到另一个工作簿上的单元格 - EPPlus

Hyperlink to a cell on another workbook - EPPlus

我想添加指向另一个工作簿的单元格的超链接。我可以引用整个文件,但在引用特定单元格时遇到问题。

生成的超链接格式正确,因为当我在生成的 Excel 文件上单击修改超链接然后确定(不修改它)时,超链接开始工作。

我已经尝试过 Uri 的所有构造函数,我尝试计算单元格值,但没有找到解决方案。这是我的代码。

这个有效:

resultSheet.Cells[currentRow, 10].Hyperlink = new Uri(message.myReference.filePath, UriKind.Absolute);

在生成的 Excel 文件上单击修改然后确定后,这才起作用。

resultSheet.Cells[currentRow, 10].Hyperlink = new Uri(message.myReference.filePath + "#'" + message.myReference.sheetName + "'!" + "A" + message.myReference.cellRow, UriKind.Absolute);

我真的很感激任何形式的帮助,因为我有点被这个愚蠢的问题困住了。

我试过使用 Interop 库,这对我有用。 也许你可以使用类似的逻辑或想法。

Range whereHyperLinkWillBe = activeWorksheet.get_Range("O3", Type.Missing);
string filePathOfHyerlinkDestination = "C:\Users\Desktop\HyerlinkFile.xlsx";
string hyperlinkTargetAddress = "Sheet1!B4";
activeWorksheet.Hyperlinks.Add(whereHyperLinkWillBe, filePathOfHyerlinkDestination ,hyperlinkTargetAddress, "Hyperlink Sample", "Hyperlink Title");

所以我找到了答案,我可以通过使用单元格的公式属性来完成,如下所示,添加超链接引用和我想在单元格上显示的值:

resultSheet.Cells[currentRow, 10].Formula = "HYPERLINK(\"" + filePath + "#'" + sheetName + "'!" + rowLetter + rowNumber + "\",\"" + "message to show on the cell" + "\")";
resultSheet.Cells[currentRow, 10].Calculate();

官方方法是使用new ExcelHyperLink(),见

ws.Cells["K13"].Hyperlink = new ExcelHyperLink("Statistics!A1", "Statistics");

https://github.com/JanKallman/EPPlus/wiki/Formatting-and-styling