Link (2)
C#/VB.NET: Extract, Modify or Remove Hyperlinks in Excel
2023-06-16 06:48:00 Written by support iceblueHyperlinks 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.
- Extract Hyperlinks from Excel in C# and VB.NET
- Modify Hyperlinks in Excel in C# and VB.NET
- Remove Hyperlinks from Excel in C# and VB.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(); } } }
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(); } } }
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(); } } }
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.
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:
- Create an instance of Workbook class.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Access the specific cell that you want to add hyperlink to using Worksheet.Range[cellName] property.
- Add a hyperlink to the cell using Worksheet.HyperLinks.Add() method.
- Set the type, display text and address for the hyperlink using XlsHyperLink.Type, XlsHyperLink.TextToDisplay and XlsHyperLink.Address properties.
- Autofit column width using XlsWorksheet.AutoFitColumn() method.
- Save the result file using Workbook.SaveToFile() method.
- 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); } } }
Add Image Hyperlinks to Excel in C# and VB.NET
The following are the steps to add an image hyperlink to Excel:
- Create an instance of Workbook class.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Insert an image into the worksheet using Worksheet.Pictures.Add() method and set column width and row height.
- Add a hyperlink to the image using XlsBitmapShape.SetHyperLink() method.
- Save the result file using Workbook.SaveToFile() method.
- 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); } } }
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.