Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight

Hyperlink

Hyperlinks enable quick access to web pages, places in the workbook or email addresses. This article demonstrates how to use the feature in terms of the API exposed by the document model.

HyperlinkCollection

Each worksheet object maintains a collection of the hyperlinks it contains. This collection can be reached through the Hyperlinks property of the Worksheet class. This property is of type HyperlinkCollection and facilitates the process of searching, adding and removing hyperlinks.

The SpreadsheetHyperlink class is the representation of hyperlink in the document model. The class exposes the following properties:

  • Range: Property of type CellRange; indicates the range of cells that holds the hyperlink info.

  • HyperlinkInfo: Property of type HyperlinkInfo; indicates the type of the hyperlink and contains information about the target of the hyperlink. There are three supported types of hyperlinks:

    • Url: The Url hyperlink refers to a page on the internet.

    • MailTo: The MailTo hyperlink contains an e-mail address and, optionally, a subject.

    • InDocument: The InDocument hyperlink holds a reference to a cell range in string format, e.g. A1:B3.

Depending on the type of the hyperlink, the HyperlinkInfo object may contain additional information about the target. The class exposes the string properties Address, EmailSubject, ScreenTip and SubAddress and each hyperlink type requires a set of these properties to be filled. Note, however, that some of them are mutually exclusive. For example, if you have an Url hyperlink, you do not need to specify the EmailSubject.

You can create instances of each of those types, using the static methods of the HyperlinkInfo class.

Example 1 creates a hyperlink to a web address.

[C#] Example 1: Create link to web address

HyperlinkInfo webAddres = HyperlinkInfo.CreateHyperlink("http://telerik.com", "Telerik");

Example 2 creates a hyperlink to a cell range somewhere in the document.

[C#] Example 2: Create link to place in the document

HyperlinkInfo inDocument = HyperlinkInfo.CreateInDocumentHyperlink("A1:B3", "Go to A1:B3");

Example 3 create a hyperlink to an email address.

[C#] Example 3: Create link to email address

HyperlinkInfo mailto = HyperlinkInfo.CreateMailtoHyperlink("someOne@someCompany.com", "someSubject", "Mail to someOne");

To add a hyperlink, you need to specify a cell range that will contain the hyperlink and a hyperlink info that will determine the type of the hyperlink.

Example 4 assigns the hyperlink created in Example 1 to A1.

[C#] Example 4: Add hyperlink

CellIndex a1Index = new CellIndex(0, 0);
SpreadsheetHyperlink spreadsheetHyperlink = worksheet.Hyperlinks.Add(a1Index, webAddres);

There are several ways you can retrieve hyperlinks from the HyperlinkCollection depending on their position relative to a given cell range.

Example 5 defines two indexes and then a cell range out of those indexes.

[C#] Example 5: Define cell range

CellIndex a1Index = new CellIndex(0, 0);
CellIndex b3Index = new CellIndex(2, 1);
CellRange a1b3Range = new CellRange(a1Index, b3Index);

Example 6 gets all hyperlinks the ranges of which are contained in the cell range from Example 5.

[C#] Example 6: Get hyperlinks in cell range

IEnumerable<SpreadsheetHyperlink> containingHyperlinks = worksheet.Hyperlinks.GetContainingHyperlinks(a1b3Range);

The GetContainingHyperlinks() method has an overload which accepts a collection of cell ranges.

Example 7 gets all hyperlinks the ranges of which intersect with the cell range from Example 5.

[C#] Example 7: Get hyperlinks intersecting with cell range

IEnumerable<SpreadsheetHyperlink> intersectingHyperlinks = worksheet.Hyperlinks.GetIntersectingHyperlinks(a1b3Range);

Example 8 gets the last added hyperlink that intersects with the cell range from Example 5.

[C#] Example 8: Get last hyperlink intersecting with cell range

bool canGetHyperlink = worksheet.Hyperlinks.TryGetHyperlink(a1Index, out spreadsheetHyperlink);

The TryGetHyperlink method has an overload that accepts a CellIndex instead of CellRange.

Example 9 gets the hyperlink which range matches the cell range from Example 5.

[C#] Example 9: Get hyperlink exactly matching cell range

bool canGetHyperlinkExact = worksheet.Hyperlinks.TryGetHyperlinkExact(a1b3Range, out spreadsheetHyperlink);

To remove a hyperlink you need to retrieve a SpreadsheetHyperlink object and then remove it from the hyperlinks collection.

Example 10 removes a hyperlink.

[C#] Example 10: Remove hyperlink

SpreadsheetHyperlink spreadsheetHyperlink;
if (worksheet.Hyperlinks.TryGetHyperlink(a1Index, out spreadsheetHyperlink))
{
    worksheet.Hyperlinks.Remove(spreadsheetHyperlink);
}