2

I`ll try to work with Excel using C#.

I need to add a hyperlink, which using the value of another cell. If this value in document is changed, hyperlink also must change.

This code:

curSheet.Hyperlinks.Add(curSheet.get_Range("c1"), "https://www.google.ru/?q=" + curSheet.get_Range("b1").Value)

will give me a fixed link. How can I create a dynamic link, which contain a sublink to cell, not cell value?

raccoon
  • 23
  • 1
  • 1
  • 5

3 Answers3

3

Use the HYPERLINK formula?

curSheet.get_Range("c1").Formula = "=HYPERLINK(""https://www.google.ru/?q="" & b1)"
Alex K.
  • 171,639
  • 30
  • 264
  • 288
3

use this code:

Excel.Worksheet sh;
sh.Hyperlinks.Add(sh.Cells[1, 1], "http://www.Sharifsoft.com/", Type.Missing, "Sharifsoft", "www.Sharifsoft.com");

I found this answer in this link

Sharif Lotfi
  • 544
  • 6
  • 13
2

you can try do below this:

 var excelApp = new Microsoft.Office.Interop.Excel.Application();
 var excelWB = excelApp.Workbooks.Add(Type.Missing);
 var excelWS = (Microsoft.Office.Interop.Excel.Worksheet)excelWB.Worksheets[1];
 var excelCell = excelWS.get_Range("A1", "A1");
 excelWS.Hyperlinks.Add(excelCell, "https://stackoverflow.com/", Type.Missing, "Stackoverflow", "Stackoverflow");
Vinson
  • 31
  • 5