Home > Articles > .Net Framework Articles > Insert Hyperlinks in Excel using Free API and C#

Insert Hyperlinks in Excel using Free API and C#

by Michael Brown   on Jan 25, 2017   Category: .Net Framework  | Level: Beginner  |  Views: 889    |  Points: 100   
Like this article? Bookmark and Share:
In this article, we'll describe how to insert hyperlinks in Excel using a free API. For instance, link to an existing web page/email address, link to a file, link to a place in workbook and link to an Unc path.


Insert Hyperlinks in Excel using Free API and C#

Introduction

In this article, we'll describe how to insert hyperlinks in Excel using a free API – Free Spire.XLS for .NET. Free Spire.XLS for .NET is a pure and independent library which enables developers to create, edit, convert, and manipulate Excel files in .NET applications without MS Excel. In the following contents, we'll explain four scenarios:

  • Link to an existing web page/email address.
  • Link to a file
  • Link to a place in workbook
  • Link to an Unc path

Download

Create a console application and install Free Spire.XLS for .NET Package through NuGet Package Manager as shown below.


Using the code

Link to an existing web page/email address

To insert web page hyperlink, we need to specify the hyperlink type to Url.

Code:  

//Load the Excel workbook

Workbook workbook = new Workbook();

workbook.LoadFromFile("test.xlsx");

//Get the worksheet

Worksheet sheet = workbook.Worksheets[0];

//Link to a web page

HyperLink UrlLink = sheet.HyperLinks.Add(sheet.Range["B3"]);

UrlLink.TextToDisplay = sheet.Range["B3"].Text;

UrlLink.Type = HyperLinkType.Url;

UrlLink.Address = "https://en.wikipedia.org/wiki/Alaska";

//Link to an email address

HyperLink MailLink = sheet.HyperLinks.Add(sheet.Range["C3"]);

MailLink.TextToDisplay = sheet.Range["C3"].Text;

MailLink.Type = HyperLinkType.Url;

MailLink.Address = "mailto:MichaelWilson@hotmail.com"; 

//Save the workbook to file

workbook.SaveToFile("UrlLink.xlsx");

Screenshot:


 

Link to a file

Code: 

//Load the Excel workbook

Workbook workbook = new Workbook();

workbook.LoadFromFile("Documents.xlsx");

//Get the worksheet

Worksheet sheet = workbook.Worksheets[0];

//Link to an existing file

CellRange range = sheet.Range["B2"];

HyperLink FileLink = sheet.HyperLinks.Add(range);

FileLink.Type = HyperLinkType.File;

FileLink.TextToDisplay = sheet.Range["B2"].Text;

FileLink.Address = @"E:\Program Files\SpireDoc.docx"; 

//Save the workbook to file

workbook.SaveToFile("FileLink.xlsx");

Screenshot:



Link to a place in workbook

Code:

//Create an Excel workbook

Workbook workbook = new Workbook();

//Get the worksheet

Worksheet sheet = workbook.Worksheets[0];

//Link to the cell B5 on Sheet 2

CellRange range = sheet.Range["B5"];

HyperLink WbLink = sheet.HyperLinks.Add(range);

WbLink.Type = HyperLinkType.Workbook;

WbLink.TextToDisplay = "Go to sheet 2";

WbLink.Address = "Sheet2!B5"; 

//Save the workbook to file

workbook.SaveToFile("Wb.xlsx", ExcelVersion.Version2013);

Screenshot:


 

Link to an Unc path

Code:

//Create an Excel workbook

Workbook workbook = new Workbook();

//Get the worksheet

Worksheet sheet = workbook.Worksheets[0];

//Link to 192.168.1.108

CellRange range = sheet.Range["D3"];

HyperLink UncLink = sheet.HyperLinks.Add(range);

UncLink.Type = HyperLinkType.Unc;

UncLink.TextToDisplay = "Go to 192.168.1.108";

UncLink.Address = "\\192.168.1.108";

//Save the workbook to file

workbook.SaveToFile("Unc.xlsx");

Screenshot:


 

 



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts