Document Solutions for Excel, .NET Edition | Document Solutions
Features / Hyperlinks
In This Topic
    Hyperlinks
    In This Topic

    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.

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

    Configure Hyperlinks

    Hyperlinks can be configured using the following properties of the IHyperlink interface.

    1. You can use the Address and SubAddress properties of the IHyperlink interface to configure the hyperlink references. The table shown below illustrates both of these properties with examples:
      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

    2. You can use the EmailSubject property to set the text of hyperlink's email subject line.
    3. You can use the ScreenTip property to set the tip text for the specified hyperlink.
    4. You can use the TextToDisplay property to set the text to be displayed for the specified hyperlink.

    Delete Hyperlinks

    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();