How to Modify Hyperlinks in Excel in C#, VB.NET

A hyperlink that directs readers from one file to another file or an address is commonly used in our electronic documents. With the help of Spire.XLS, programmers can easily insert or remove hyperlinks in Excel sheet. In this article, you'll learn how to modify a hyperlink including the address and hyperlink text in Excel via Spire.XLS in C#, VB.NET.

Screenshot of sample file:

How to Modify Hyperlinks in Excel in C#, VB.NET

Code Snippet:

Step 1: Initialize a new instance of workbook class, load the sample file from disk.

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

Step 2: Get the collection of all hyperlinks in the worksheet.

HyperLinksCollection links = sheet.HyperLinks;

Step 3: Change the values of TextToDisplay and Address property of HyperLinksCollection class to reset the hyperlink text and address.

links[0].TextToDisplay = "Spire.XLS for .NET";
links[0].Address = "http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html";

Step 4: Save and launch the file.

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

Output:

How to Modify Hyperlinks in Excel in C#, VB.NET

Full Code:

[C#]
Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];    

HyperLinksCollection links = sheet.HyperLinks;
links[0].TextToDisplay = "Spire.XLS for .NET";
links[0].Address = "http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html";

workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");
[VB.NET]
Dim workbook As New Workbook()
workbook.LoadFromFile("sample.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)

Dim links As HyperLinksCollection = sheet.HyperLinks
links(0).TextToDisplay = "Spire.XLS for .NET"
links(0).Address = "http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html"

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