How to Insert Hyperlink in Excel for WPF Applications

2016-03-11 07:43:55 Written by  support iceblue
Rate this item
(0 votes)

Generally, a hyperlink is a picture or a piece of text which contains a web link that points to another object. The object can be a website, an email address and a file, etc. By clicking on the hyperlink, readers can access the target place quickly and conveniently.

This article demonstrates how to insert hyperlink in excel for WPF Applications by using Spire.XLS for WPF.

Below is the effective screenshot:

How to Insert Hyperlink in Excel for WPF Applications

In this sample, we added two hyperlinks: one for cell A5, another for cell D13. Now, refer to the following detail steps:

Code snippets:

Use namespace:

using System.Windows;
using Spire.Xls;

Step 1: Initialize a new instance of Workbook class and load the excel document from file, then get its first worksheet.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Vendors Information.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Step 2: In the first worksheet, add hyperlink to cell A5, set its display text as the existing text of cell A5, next set type and address.

HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]);
Link.TextToDisplay = sheet.Range["A5"].Text;
Link.Type = HyperLinkType.Url;
Link.Address = "https://en.wikipedia.org/wiki/Canada";

Step 3: Add a new hyperlink to cell D13 and set its display text, type and address.

HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]);
NewLink.TextToDisplay = "https://www.google.com";
NewLink.Type = HyperLinkType.Url;
NewLink.Address = "https://www.google.com";

Step 4: Save and launch the file.

workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("Hyperlink.xlsx");

Full codes:

[C#]
private void button1_Click(object sender, RoutedEventArgs e)
{
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("Vendors Information.xlsx");
    Worksheet sheet = workbook.Worksheets[0];

    HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]);
    Link.TextToDisplay = sheet.Range["A5"].Text;
    Link.Type = HyperLinkType.Url;
    Link.Address = "https://en.wikipedia.org/wiki/Canada";

    HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]);
    NewLink.TextToDisplay = "https://www.google.com";
    NewLink.Type = HyperLinkType.Url;
    NewLink.Address = "https://www.google.com";

    workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010);
    System.Diagnostics.Process.Start("Hyperlink.xlsx");
}
[VB.NET]
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
	Dim workbook As New Workbook()
	workbook.LoadFromFile("Vendors Information.xlsx")
	Dim sheet As Worksheet = workbook.Worksheets(0)

	Dim Link As HyperLink = sheet.HyperLinks.Add(sheet.Range("A5"))
	Link.TextToDisplay = sheet.Range("A5").Text
	Link.Type = HyperLinkType.Url
	Link.Address = "https://en.wikipedia.org/wiki/Canada"

	Dim NewLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("D13"))
	NewLink.TextToDisplay = "https://www.google.com"
	NewLink.Type = HyperLinkType.Url
	NewLink.Address = "https://www.google.com"

	workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010)
	System.Diagnostics.Process.Start("Hyperlink.xlsx")
End Sub

Additional Info

  • tutorial_title: Insert Hyperlink in Excel for WPF Applications
Last modified on Friday, 24 September 2021 09:48