How to Create Hyperlinks in Excel Using C#

In this tutorial, viewers learn how to set up hyperlinks in Excel files using the IronXL library. The process begins with ensuring the IronXL library is installed via the NuGet package manager. The tutorial walks through a basic script that includes using statements for accessing IronXL features, creating workbooks, and worksheets. It explains how to set a hyperlink in cell A1 to the ironpdf.com website and how to navigate within the same worksheet by clicking on cell A1 to jump to cell Z20. Additionally, it demonstrates setting hyperlinks across different worksheets, such as linking cell A3 to cell A1 of sheet 2, and opening external Excel files by clicking on cell A5. The tutorial concludes by running the project to verify that links work as expected, showcasing the powerful interactivity features that IronXL offers for Excel files. Viewers are encouraged to subscribe for more tutorials and explore the software by following the provided link.

Sample Code

Here's an example script demonstrating how to create hyperlinks in Excel with IronXL.

// Include IronXL namespace for Excel operations
using IronXL;

class HyperlinksExample
{
    static void Main()
    {
        // Load the existing workbook or create a new one
        WorkBook workbook = WorkBook.LoadOrCreate("Example.xlsx");

        // Select the first worksheet or create a new one
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Add a hyperlink to an external website
        sheet["A1"].Hyperlink = new HyperLink("https://ironpdf.com", "Visit IronPDF");

        // Add an internal hyperlink to navigate within the same worksheet
        sheet["A2"].Value = "Go to Z20";
        sheet["A1"].Hyperlink = new HyperLink("Z20");

        // Create a new worksheet or fetch if it already exists
        WorkSheet sheet2 = workbook.GetWorkSheet("Sheet2") ?? workbook.CreateWorkSheet("Sheet2");

        // Add hyperlink to another worksheet cell
        sheet["A3"].Hyperlink = new HyperLink(sheet2["A1"]);

        // Add hyperlink to open an external Excel file (assuming 'External.xlsx' exists)
        sheet["A5"].Hyperlink = new HyperLink("External.xlsx", "Open External File");

        // Save changes to the Excel file
        workbook.Save();
    }
}
// Include IronXL namespace for Excel operations
using IronXL;

class HyperlinksExample
{
    static void Main()
    {
        // Load the existing workbook or create a new one
        WorkBook workbook = WorkBook.LoadOrCreate("Example.xlsx");

        // Select the first worksheet or create a new one
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Add a hyperlink to an external website
        sheet["A1"].Hyperlink = new HyperLink("https://ironpdf.com", "Visit IronPDF");

        // Add an internal hyperlink to navigate within the same worksheet
        sheet["A2"].Value = "Go to Z20";
        sheet["A1"].Hyperlink = new HyperLink("Z20");

        // Create a new worksheet or fetch if it already exists
        WorkSheet sheet2 = workbook.GetWorkSheet("Sheet2") ?? workbook.CreateWorkSheet("Sheet2");

        // Add hyperlink to another worksheet cell
        sheet["A3"].Hyperlink = new HyperLink(sheet2["A1"]);

        // Add hyperlink to open an external Excel file (assuming 'External.xlsx' exists)
        sheet["A5"].Hyperlink = new HyperLink("External.xlsx", "Open External File");

        // Save changes to the Excel file
        workbook.Save();
    }
}
' Include IronXL namespace for Excel operations
Imports IronXL

Friend Class HyperlinksExample
	Shared Sub Main()
		' Load the existing workbook or create a new one
		Dim workbook As WorkBook = WorkBook.LoadOrCreate("Example.xlsx")

		' Select the first worksheet or create a new one
		Dim sheet As WorkSheet = workbook.DefaultWorkSheet

		' Add a hyperlink to an external website
		sheet("A1").Hyperlink = New HyperLink("https://ironpdf.com", "Visit IronPDF")

		' Add an internal hyperlink to navigate within the same worksheet
		sheet("A2").Value = "Go to Z20"
		sheet("A1").Hyperlink = New HyperLink("Z20")

		' Create a new worksheet or fetch if it already exists
		Dim sheet2 As WorkSheet = If(workbook.GetWorkSheet("Sheet2"), workbook.CreateWorkSheet("Sheet2"))

		' Add hyperlink to another worksheet cell
		sheet("A3").Hyperlink = New HyperLink(sheet2("A1"))

		' Add hyperlink to open an external Excel file (assuming 'External.xlsx' exists)
		sheet("A5").Hyperlink = New HyperLink("External.xlsx", "Open External File")

		' Save changes to the Excel file
		workbook.Save()
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Explanation

  • Include IronXL namespace: It allows access to the functionalities required to manipulate Excel files using IronXL.
  • Load or Create a Workbook: This function will either load an existing Excel file or create a new one if it doesn't exist.
  • Worksheet operations: Retrieves the default worksheet and allows the creation of new worksheets if needed.
  • Hyperlink creation:
    • Cell A1 is linked to an external website, with a clickable text "Visit IronPDF".
    • Cell A2 is configured to navigate to cell Z20 within the same worksheet.
    • Cell A3 links to cell A1 on Sheet2, allowing easy navigation between sheets.
    • Cell A5 opens an external Excel file named 'External.xlsx', adding a new layer of interactivity.
  • Save changes: After all hyperlinks have been set, the workbook is saved to ensure all modifications are written to the file.

Further Reading: How to Create Hyperlink

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Load Existing Spreadsheets in C#
NEXT >
How to Group and Ungroup Rows and Columns in Excel