C#/VB.NET: Extract, Modify or Remove Hyperlinks in Excel

Hyperlinks are an essential element in Excel that allows users to reference external data sources, navigate between worksheets, or provide additional information about specific cells. When working with an Excel file, you may need to manipulate hyperlinks for various reasons. For example, you may need to extract all the hyperlinks from the file to perform an analysis, modify an outdated hyperlink to ensure accuracy or remove a broken hyperlink to improve the document's usability. In this article, we will explain how to extract, modify and remove hyperlinks in Excel in C# and VB.NET using Spire.XLS for .NET.

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

Extract Hyperlinks from Excel in C# and VB.NET

If you are migrating data from an Excel workbook to another system (such as a database) and need to preserve the hyperlinks associated with that data, extracting hyperlinks from the Excel file beforehand is necessary.

The following steps demonstrate how to extract hyperlinks from an Excel file in C# and VB.NET using Spire.XLS for .NET:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get a specific worksheet using the Workbook.Worksheets[int index] property.
  • Get the collection of all hyperlinks in the worksheet using the Worksheet.Hyperlinks property.
  • Initialize an instance of the StringBuilder class to store the extracted hyperlink information.
  • Iterate through the hyperlinks in the hyperlinks collection.
  • Get the address and type of each hyperlink using the XlsHyperlink.Address and XlsHyperlink.Type properties.
  • Append the address and type to the StringBuilder instance.
  • Write the content of the StringBuilder instance into a text file using the File.WriteAllText() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core.Spreadsheet;
using System.IO;
using System.Text;

namespace ExtractHyperlinks
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Hyperlinks1.xlsx");

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

            //Get the collection of all hyperlinks in the worksheet
            HyperLinksCollection hyperLinks = sheet.HyperLinks;
           
            //Initialize an instance of the StringBuilder class
            StringBuilder sb = new StringBuilder();

            //Iterate through the hyperlinks in the collection
            foreach (XlsHyperLink hyperlink in hyperLinks)
            {
                //Get the address of the hyperlink
                string address = hyperlink.Address;
                //Get the type of the hyperlink
                HyperLinkType type = hyperlink.Type;
                //Append the address and type of the hyperlink to the StringBuilder instance
                sb.AppendLine("Link address: " + address);
                sb.AppendLine("Link type: " + type.ToString());               
                sb.AppendLine();
            }

            //Write the content of the StringBuilder instance to a text file
            File.WriteAllText("GetHyperlinks.txt", sb.ToString());
            workbook.Dispose();
        }
    }
}

C#/VB.NET: Extract, Modify or Remove Hyperlinks in Excel

Modify Hyperlinks in Excel in C# and VB.NET

If you've accidentally linked to the wrong resource or entered an incorrect URL when creating a hyperlink, you may need to modify the hyperlink to correct the mistake.

The following steps demonstrate how to modify an existing hyperlink in an Excel file:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get a specific worksheet using the Workbook.Worksheets[int index] property.
  • Get the collection of all hyperlinks in the worksheet using the Worksheet.Hyperlinks property.
  • Get the first hyperlink in the collection.
  • Modify the display text and address of the hyperlink using the XlsHyperlink.TextToDisplay and XlsHyperlink.Address properties.
  • Save the result file using the Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core.Spreadsheet;

namespace ModifyHyperlinks
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Hyperlinks2.xlsx");

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

            //Get the collection of all hyperlinks in the worksheet
            HyperLinksCollection links = sheet.HyperLinks;
            //Get the first hyperlink in the collection
            XlsHyperLink hyperLink = links[0];

            //Modify the display text and the address of the hyperlink
            hyperLink.TextToDisplay = "Spire.XLS for .NET";
            hyperLink.Address = "http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html";

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

C#/VB.NET: Extract, Modify or Remove Hyperlinks in Excel

Remove Hyperlinks from Excel in C# and VB.NET

Removing the irrelevant hyperlinks can help make your worksheet neater and more professional-looking.

The following steps demonstrate how to remove a specific hyperlink from an Excel file:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get a specific worksheet using the Workbook.Worksheets[int index] property.
  • Remove a specific hyperlink from the worksheet using the Worksheet.Hyperlinks.RemoveAt(int index) method.
  • Save the result file using the Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace RemoveHyperlinks
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Hyperlinks2.xlsx");

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

            //Remove the first hyperlink and keep its display text
            sheet.HyperLinks.RemoveAt(0);

            //Remove all content from the cell
            //sheet.Range["B2"].ClearAll();

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

C#/VB.NET: Extract, Modify or Remove Hyperlinks in 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.