How to Add Image Hyperlink in Excel in C#, VB.NET

Hyperlinks can direct readers from one file to a web address, an email address or another file. A Hyperlink can be added to a text or an image. In our previous articles, we have demonstrated how to add text hyperlink to Excel worksheet. This article will represent how hyperlink is created based on an image in C# and VB.NET.

Code Snippet:

Step 1: Create an object of Workbook and get the first worksheet.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Insert an image to a specific cell.

string picPath = @"C:\Users\Administrator\Desktop\logo.png";
ExcelPicture picture = sheet.Pictures.Add(1, 1, picPath);

Step 3: Add a hyperlink to the image using SetHyperLink(string linkString, bool isExternal) method.

picture.SetHyperLink("Http://www.e-iceblue.com", true);

Step 4: Set the 1st column width and 1st row height.

sheet.Columns[0].ColumnWidth = 22;
sheet.Rows[0].RowHeight = 140;

Step 5: Save the file.

wb.SaveToFile("ImageHyperlink.xlsx", ExcelVersion.Version2013);

Output:

How to Add Image Hyperlink in Excel in C#, VB.NET

Full Code:

[C#]
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

sheet.Range["A1"].Text = "Image Hyperlink";
sheet.Range["A1"].Style.VerticalAlignment = VerticalAlignType.Top;

string picPath = @"C:\Users\Administrator\Desktop\logo.png";
ExcelPicture picture = sheet.Pictures.Add(1, 1, picPath);
picture.SetHyperLink("Http://www.e-iceblue.com", true);

sheet.Columns[0].ColumnWidth = 22;
sheet.Rows[0].RowHeight = 140;
picture.TopRowOffset = 25;

wb.SaveToFile("ImageHyperlink.xlsx", ExcelVersion.Version2013);
[VB.NET]
Dim wb As New Workbook()
Dim sheet As Worksheet = wb.Worksheets(0)

sheet.Range("A1").Text = "Image Hyperlink"
sheet.Range("A1").Style.VerticalAlignment = VerticalAlignType.Top

Dim picPath As String = "C:\Users\Administrator\Desktop\logo.png"
Dim picture As ExcelPicture = sheet.Pictures.Add(1, 1, picPath)
picture.SetHyperLink("Http://www.e-iceblue.com", True)

sheet.Columns(0).ColumnWidth = 22
sheet.Rows(0).RowHeight = 140
picture.TopRowOffset = 25

wb.SaveToFile("ImageHyperlink.xlsx", ExcelVersion.Version2013)