When demonstrating an excel report, you may also want to share information from other external files or websites. In Excel, we can add both URL hyperlinks and external files by right-clicking on cells, selecting hyperlink and then adding URL address or choosing files from disk. This article is aimed to explain how to add hyperlinks to external files in excel programmatically using Spire.XLS for WPF. To add URL hyperlinks, please refer to this article: How to Insert Hyperlink in Excel for WPF Applications.
Please see the effective screenshot below after adding hyperlinks to external files:
Code Snippets:
Use the following namespace:
using System.Windows; using Spire.Xls;
Step 1: Initialize a new Workbook object, load the sample excel file and get its first worksheet.
Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); Worksheet sheet = workbook.Worksheets[0];
Step 2: Get the cell/cell range that you want to add hyperlink to, then call the sheet.HyperLinks.Add(CellRange range) method to add the hyperlink to the cell/cell range.
CellRange range1 = sheet.Range["D18"]; HyperLink hyperlink1 = sheet.HyperLinks.Add(range1);
Step 3: Specify the hyperlink style and the hyperlink target, here we set its style to file and target to an external excel file.
hyperlink1.Type = HyperLinkType.File; hyperlink1.Address = "SalesInfo.xlsx";
Step 4: Repeat step 2 and step 3 to add a hyperlink to another specific cell, set the hyperlink style to file and set its target to a word file.
CellRange range2 = sheet.Range["E18"]; HyperLink hyperlink2 = sheet.HyperLinks.Add(range2); hyperlink2.Type = HyperLinkType.File; hyperlink2.Address = "Report.doc";
Step 5: Save and launch the file.
workbook.SaveToFile("LinktoFile.xlsx", FileFormat.Version2010); System.Diagnostics.Process.Start("LinktoFile.xlsx");
Full Codes:
using Spire.Xls; using System.Windows; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Vendors Information.xlsx"); Worksheet sheet = workbook.Worksheets[0]; HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]); Link.TextToDisplay = sheet.Range["A5"].Text; Link.Type = HyperLinkType.Url; Link.Address = "https://en.wikipedia.org/wiki/Canada"; HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]); NewLink.TextToDisplay = "https://www.google.com"; NewLink.Type = HyperLinkType.Url; NewLink.Address = "https://www.google.com"; workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("Hyperlink.xlsx"); } } }
Imports Spire.Xls Imports System.Windows Namespace WpfApplication1 Public Partial Class MainWindow Inherits Window Public Sub New() InitializeComponent() End Sub Private Sub button1_Click(sender As Object, e As RoutedEventArgs) Dim workbook As New Workbook() workbook.LoadFromFile("Vendors Information.xlsx") Dim sheet As Worksheet = workbook.Worksheets(0) Dim Link As HyperLink = sheet.HyperLinks.Add(sheet.Range("A5")) Link.TextToDisplay = sheet.Range("A5").Text Link.Type = HyperLinkType.Url Link.Address = "https://en.wikipedia.org/wiki/Canada" Dim NewLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("D13")) NewLink.TextToDisplay = "https://www.google.com" NewLink.Type = HyperLinkType.Url NewLink.Address = "https://www.google.com" workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010) System.Diagnostics.Process.Start("Hyperlink.xlsx") End Sub End Class End Namespace