C#/VB.NET: Add Hyperlinks to Excel

A hyperlink in Excel is a clickable text or an image that navigates to a specific location, such as a webpage, an existing file, an email address, or another cell in the current workbook. This article will demonstrate how to add hyperlinks to Excel in C# and VB.NET using Spire.XLS for .NET library.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add Text Hyperlinks to Excel in C# and VB.NET

The following are the steps to add a text hyperlink to Excel:

  • C#
  • VB.NET
using Spire.Xls;

namespace AddTextHyperlinks
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Add a text hyperlink that leads to a webpage
            CellRange cell1 = sheet.Range["B3"];
            HyperLink urlLink = sheet.HyperLinks.Add(cell1);
            urlLink.Type = HyperLinkType.Url;
            urlLink.TextToDisplay = "Link to a website";
            urlLink.Address = "https://www.google.com/";

            //Add a text hyperlink that leads to an email address
            CellRange cell2 = sheet.Range["E3"];
            HyperLink mailLink = sheet.HyperLinks.Add(cell2);
            mailLink.Type = HyperLinkType.Url;
            mailLink.TextToDisplay = "Link to an email address";
            mailLink.Address = "mailto:abc@outlook.com";

            //Add a text hyperlink that leads to an external file
            CellRange cell3 = sheet.Range["B7"];
            HyperLink fileLink = sheet.HyperLinks.Add(cell3);
            fileLink.Type = HyperLinkType.File;
            fileLink.TextToDisplay = "Link to an external file";
            fileLink.Address = "C:\\Users\\Administrator\\Desktop\\Report.xlsx";

            //Add a text hyperlink that leads to a cell in another sheet
            CellRange cell4 = sheet.Range["E7"];
            HyperLink linkToSheet = sheet.HyperLinks.Add(cell4);
            linkToSheet.Type = HyperLinkType.Workbook;
            linkToSheet.TextToDisplay = "Link to a cell in sheet2";
            linkToSheet.Address = "Sheet2!B5";

            //Add a text hyperlink that leads to a UNC address
            CellRange cell5 = sheet.Range["B11"];
            HyperLink uncLink = sheet.HyperLinks.Add(cell5);
            uncLink.Type = HyperLinkType.Unc;
            uncLink.TextToDisplay = "Link to a UNC address";
            uncLink.Address = "\\\\192.168.0.121";

            //Autofit column width
            sheet.AutoFitColumn(2);
            sheet.AutoFitColumn(5);

            //Save the result file
            workbook.SaveToFile("AddTextHyperlinks.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Add Hyperlinks to Excel

Add Image Hyperlinks to Excel in C# and VB.NET

The following are the steps to add an image hyperlink to Excel:

  • C#
  • VB.NET
using Spire.Xls;

namespace AddImageHyperlinks
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Get the first worksheet 
            Worksheet sheet = workbook.Worksheets[0];

            //Insert an image into the worksheet
            ExcelPicture picture = sheet.Pictures.Add(5, 3, "Logo.png");
            sheet.Columns[2].ColumnWidth = 11;
            sheet.Rows[4].RowHeight = 60;

            //Add a hyperlink to the image 
            picture.SetHyperLink("https://www.e-iceblue.com", true);
            
            //Save the result file
            workbook.SaveToFile("AddImageHyperlink.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Add Hyperlinks to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.