Retrieve Hyperlinks from an Excel Sheet in C#, VB.NET

Spire.XLS supports to insert hyperlinks in Excel, and modify or remove hyperlinks in an existing Excel document. This article presents how to retrieve hyperlink information from an Excel worksheet in C# and VB.NET.

Step 1: Create a Workbook object and load a sample Excel file.

Workbook wb = new Workbook();
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Hyperlinks.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = wb.Worksheets[0];

Step 3: Get the hyperlink information from the worksheet.

foreach (var item in sheet.HyperLinks)
{
    string address = item.Address;
    CellRange range = item.Range;       
    Console.WriteLine(string.Format("Cell[{0},{1}] contains URL: {2}", range.Row, range.Column, address));
}

Output:

Retrieve Hyperlinks from an Excel Sheet in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using System;
namespace RetrieveHyperlink
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Hyperlinks.xlsx");

            Worksheet sheet = wb.Worksheets[0];
            foreach (var item in sheet.HyperLinks)
            {
                string address = item.Address;
                CellRange range = item.Range;
                Console.WriteLine(string.Format("Cell[{0},{1}] contains URL: {2}", range.Row, range.Column, address));
            }


        }
    }
}
[VB.NET]
Imports Spire.Xls
Namespace RetrieveHyperlink
	Class Program

		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			wb.LoadFromFile("C:\Users\Administrator\Desktop\Hyperlinks.xlsx")

			Dim sheet As Worksheet = wb.Worksheets(0)
			For Each item As var In sheet.HyperLinks
				Dim address As String = item.Address
				Dim range As CellRange = item.Range
				Console.WriteLine(String.Format("Cell[{0},{1}] contains URL: {2}", range.Row, range.Column, address))
			Next


		End Sub
	End Class
End Namespace