Spire.XLS组件为开发人员提供了多种超链接类型,如网页超链接,文档超链接等。这些类型被定义在枚举Spire.Xls.HyperLinkType中,如下图所示:
网页超链接
C#
//加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//添加网页超链接
HyperLink UrlLink = sheet.HyperLinks.Add(sheet.Range["B3"]);
UrlLink.TextToDisplay = sheet.Range["B3"].Text;
UrlLink.Type = HyperLinkType.Url;
UrlLink.Address = "https://www.e-iceblue.com/Introduce/excel-for-net-introduce.html";
//添加邮件地址超链接
HyperLink MailLink = sheet.HyperLinks.Add(sheet.Range["C3"]);
MailLink.TextToDisplay = sheet.Range["C3"].Text;
MailLink.Type = HyperLinkType.Url;
MailLink.Address = "mailto:support @ e-iceblue.com";
//保存文档
workbook.SaveToFile("UrlLink.xlsx");
VB.NET
'加载Excel文档
Dim workbook As New Workbook()
workbook.LoadFromFile("test.xlsx")
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'添加网页超链接
Dim UrlLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("B3"))
UrlLink.TextToDisplay = sheet.Range("B3").Text
UrlLink.Type = HyperLinkType.Url
UrlLink.Address = "https://www.e-iceblue.com/Introduce/excel-for-net-introduce.html"
'添加邮件地址超链接
Dim MailLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("C3"))
MailLink.TextToDisplay = sheet.Range("C3").Text
MailLink.Type = HyperLinkType.Url
MailLink.Address = "mailto:support @ e-iceblue.com"
'保存文档
workbook.SaveToFile("UrlLink.xlsx")
文档超链接
C#
//加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("Documents.xlsx");
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//添加文档超链接
CellRange range = sheet.Range["B2"];
HyperLink FileLink = sheet.HyperLinks.Add(range);
FileLink.Type = HyperLinkType.File;
FileLink.TextToDisplay = sheet.Range["B2"].Text;
FileLink.Address = @"E:\Program Files\SpireDoc.docx";
//保存文档
workbook.SaveToFile("FileLink.xlsx");
VB.NET
'加载Excel文档
Dim workbook As New Workbook()
workbook.LoadFromFile("Documents.xlsx")
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'添加文档超链接
Dim range As CellRange = sheet.Range("B2")
Dim FileLink As HyperLink = sheet.HyperLinks.Add(range)
FileLink.Type = HyperLinkType.File
FileLink.TextToDisplay = sheet.Range("B2").Text
FileLink.Address = "E:\Program Files\SpireDoc.docx"
'保存文档
workbook.SaveToFile("FileLink.xlsx")
链接到本文档内的指定位置
C#
//新建Excel文档
Workbook workbook = new Workbook();
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//链接至本文档内的第二个工作表的B5单元格
CellRange range = sheet.Range["B5"];
HyperLink WbLink = sheet.HyperLinks.Add(range);
WbLink.Type = HyperLinkType.Workbook;
WbLink.TextToDisplay = "Go to sheet 2";
WbLink.Address = "Sheet2!B5";
//保存文档
workbook.SaveToFile("Wb.xlsx", ExcelVersion.Version2013);
VB.NET
'新建Excel文档
Dim workbook As New Workbook()
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'链接至本文档内的第二个工作表的B5单元格
Dim range As CellRange = sheet.Range("B5")
Dim WbLink As HyperLink = sheet.HyperLinks.Add(range)
WbLink.Type = HyperLinkType.Workbook
WbLink.TextToDisplay = "Go to sheet 2"
WbLink.Address = "Sheet2!B5"
'保存文档
workbook.SaveToFile("Wb.xlsx", ExcelVersion.Version2013)
链接到Unc路径(网络路径)
C#
//新建Excel文档
Workbook workbook = new Workbook();
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//链接到192.168.1.108
CellRange range = sheet.Range["D3"];
HyperLink UncLink = sheet.HyperLinks.Add(range);
UncLink.Type = HyperLinkType.Unc;
UncLink.TextToDisplay = "Go to 192.168.1.108";
UncLink.Address = "\\192.168.1.108";
//保存文档
workbook.SaveToFile("Unc.xlsx");
VB.NET
'新建Excel文档
Dim workbook As New Workbook()
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'链接到192.168.1.108
Dim range As CellRange = sheet.Range("D3")
Dim UncLink As HyperLink = sheet.HyperLinks.Add(range)
UncLink.Type = HyperLinkType.Unc
UncLink.TextToDisplay = "Go to 192.168.1.108"
UncLink.Address = "\192.168.1.108"
'保存文档
workbook.SaveToFile("Unc.xlsx")