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")