Pablo Maldonado
Pablo Maldonado

Reputation: 1

Hyperlink to a cell on another workbook - EPPlus

I want to add an hyperlink to a cell of another workbook. I am able to refer the whole file but I have issues when referring an specific cell.

The generated hyperlink is well formed because when I click on modify hyperlink and then OK (without modifying it) on the generated Excel file then the hyperlink starts to work.

I have already tried with all the constructors of Uri, I tried to calculate the cell value but I don't find the solution. Here's my code.

This works:

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

This doesn't work until clicking on modify and then ok on the generated Excel file.

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

I would really appreciate any kind of help because I'm kinda stuck on this silly issue.

Upvotes: 0

Views: 1751

Answers (3)

LoJo
LoJo

Reputation: 142

The official method is to use new ExcelHyperLink(), see

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

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

Upvotes: 1

Pablo Maldonado
Pablo Maldonado

Reputation: 1

So I found the answer, I was able to do it by using the formula attribute of the cell as follow by adding the hyperlink reference and the value that I wanted to show on the cell:

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

Upvotes: 0

hackslash47
hackslash47

Reputation: 71

I've tried using the Interop library and this works for me. Maybe you can use a similar sort of logic or idea.

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");

Upvotes: 1

Related Questions