DsExcel .NET allows users to create references to the data in the form of hypertext links that point towards another document or a section within the same document. A worksheet or a range can have multiple hyperlinks. Hyperlinks can be created and inserted in cells to allow users to quickly access related information present in another file or on a webpage by clicking on the link.
Hyperlinks are stored in a specific worksheet or in a range by accessing the Hyperlinks collection of the IWorksheet interface and the IRange interface respectively.
You can perform the following tasks to manage hyperlinks.
Hyperlinks can be created and inserted through linking to an external file, linking to a webpage, linking to an email address and also linking to a range within the worksheet. You can add hyperlinks for a range of cells in a worksheet using the Add method of the IHyperLinks interface.
Refer to the following example code to insert hyperlinks to an external file, to a webpage, to a range within the worksheet and to an email address.
C# |
Copy Code |
---|---|
// Add a hyperlink link to external file worksheet.Range["A1:B2"].Hyperlinks.Add(worksheet.Range["A1"], "C:\Documents\Project\Hyperlink\SampleFile.xlsx", null, "link to SampleFile.xlsx file.", "SampleFile.xlsx"); |
C# |
Copy Code |
---|---|
// Add a hyperlink link to web page worksheet.Range["A1:B2"].Hyperlinks.Add(worksheet.Range["A1"], "https://developer.mescius.com/", null, "open MESCIUS, Inc. web site.", "MESCIUS, Inc."); |
C# |
Copy Code |
---|---|
//Add a hyperlink link to a range in this document. worksheet.Range["A1:B2"].Hyperlinks.Add(worksheet.Range["A1"], null, "Sheet1!$C$3:$E$4", "Go To sheet1 C3:E4"); |
C# |
Copy Code |
---|---|
//Add a hyperlink link to email address. worksheet.Range["A1:B2"].Hyperlinks.Add(worksheet.Range["A1"], "mailto:abc.xyz@mescius.com", null, "Send an email to ABC", "Send To ABC"); |
Hyperlinks can be configured using the following properties of the IHyperlink interface.
Link To | Address | SubAddress |
---|---|---|
External File | Example: "C:\Users\Desktop\test.xlsx" | null |
Webpage | Example: "https://developer.mescius.com/" | null |
A range in this document | Example: null | "Sheet1!$C$3:$E$4" |
Email Address | Example: "mailto: abc.xyz@mescius.com" | null |
The hyperlinks inserted in the cells can be removed from the hyperlinks collection in a specific worksheet or in a specific range using the Delete method.
Refer to the following example code to delete hyperlinks.
C# |
Copy Code |
---|---|
//Delete hyperlinks. worksheet.Range["A1:B2"].Hyperlinks.Add(worksheet.Range["A1:A2"], null, "Sheet1!$C$3:$E$4", "Go To sheet1 C3:E4"); worksheet.Range["H5"].Hyperlinks.Add(worksheet.Range["A1"], "https://developer.mescius.com/"); worksheet.Range["J6"].Hyperlinks.Add(worksheet.Range["A1"], "https://developer.mescius.com/"); //delete hyperlinks in range A1:A2. worksheet.Range["A1:A2"].Hyperlinks.Delete(); //delete all hyperlinks in this worksheet. worksheet.Hyperlinks.Delete(); |