There are two types of hyperlinks: one is URL type, which linked to any web protocol; the other is file path type, which usually point to a file in a folder. Before we have discussed how to manage URL hyperlinks in Excel via C#/VB.NET, thus this section will demonstrate how to manage Excel hyperlinks, which target at a file path.
Spire.Xls for .NET provides Hyperlink type property in its class design, which makes the operation related to Excel hyperlinks in C#/VB.NET quite simple and smooth for developers and programmers.
Spire.Xls for .NET is always welcome to any kind of trial and evaluation. So now please feel free to download Spire.Xls for .NET. And then follow our guide to add Excel hyperlinks in C#/VB.NET or try other function of Spire.Xls for .NET.
Three Steps of adding Excel hyperlinks in C#/VB.NET
Step 1: Create new project and add reference
In this step, we create a new project and add spire.xls dll as its reference. The following operations relate to Excel hyperlinks are all preceded within this project.
Step 2: Import namespace.
Spire.Xls is a namespace which must be used during our procedure of adding Excel hyperlinks via C#/VB.NET, so please use the code below to import it.
Step 3: Add an Excel hyperlink to a cell
In this step, we will load an Excel file from a folder and then add a hyperlink to a cell in C#/VB.NET. This hyperlink will link to a Excel file by setting its type. Have a check of the code sample.
//load Excel file Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\hyperlink.xlsx", ExcelVersion.Version2010); // add hyperlink and make setting Worksheet sheet1 = workbook.Worksheets; CellRange range = sheet1.Range["C3"]; HyperLink hyperlink1 = sheet1.HyperLinks.Add(range); hyperlink1.Type = HyperLinkType.File; hyperlink1.TextToDisplay = "This is a hyperlink to a target file” hyperlink1.Address = "employee2.xlsx";
'load Excel file Dim workbook As New Workbook() workbook.LoadFromFile("..\..\hyperlink.xlsx", ExcelVersion.Version2010) ' add hyperlink Dim sheet1 As Worksheet = workbook.Worksheets(0) Dim range As CellRange = sheet1.Range("C3") Dim hyperlink1 As HyperLink = sheet1.HyperLinks.Add(range) hyperlink1.Type = HyperLinkType.File hyperlink1.TextToDisplay = "This is a hyperlink" hyperlink1.Address = "employee2.xlsx"
Finally, we can save our work and run it to preview the effect. The effect before we add Excel hyperlink in C#/VB.NET:
The effect after we add Excel hyperlink in C#/VB.NET:
You can see a hyperlink has been added in Excel, and if you click on the hyperlink, it will lead you to a new Excel file named employee2.xlsx.