Skip to main content

Use the Excel Export API to Add a Hyperlink to a Cell

  • 4 minutes to read

The Excel Export Library allows you to create hyperlinks represented by the XlHyperlink objects. All hyperlinks in a worksheet are contained in a collection returned by the IXlSheet.Hyperlinks property.

To create a hyperlink, follow the steps below:

  1. Initialize a new instance of the XlHyperlink class using the default XlHyperlink constructor.
  2. Specify the cell or cell range to which the hyperlink should be attached by using the XlHyperlink.Reference property.
  3. Use the XlHyperlinkBase.TargetUri property to specify the hyperlink destination. The following table lists possible locations to which hyperlinks can refer.

    Hyperlink Destination

    TargetUri Value

    Example

    Place in the current workbook

    A cell reference or defined name preceded by the worksheet name.

    “#Sheet1!B4”

    Place in an external workbook

    A cell reference or defined name preceded by the path to a file, the workbook and worksheet names.

    If a path to the destination workbook is not specified, the path relative to the location of the current workbook will be used.

    “D:\Shared\Expenses.xlsx#Sheet1!C5”

    “Document.xls#Sheet1!B4”

    Web page

    A web address.

    “https://www.devexpress.com/“

    E-mail address

    An e-mail address preceded by the “mailto:” prefix.

    mailto:support@devexpress.com

    Existing File or Directory

    A path to a file and file name, or a path to a directory.

    A path can be absolute or relative to the directory where the current workbook is located.

    “D:\Pictures\Image1.png”

    “..\..\..\MyBook.xlsx”

  4. Add a newly created hyperlink to the collection of hyperlinks contained in a worksheet.

    If a cell range to which you added a hyperlink does not contain any value, you can provide a display text for it to indicate that this cell range has a hyperlink attached. To do this, create a cell that should contain hyperlink text. Normally, it is the top-left cell of the range associated with a hyperlink. Set the cell’s IXlCell.Value property to the string value that specifies the text you wish to use to represent the hyperlink. Format the specified text as a hyperlink by setting the IXlCell.Formatting property to the XlCellFormatting.Hyperlink object.

The code snippet below illustrates how to create a hyperlink to cells located in the same and external workbooks, and a web page.

View Example

// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet())
{
    using(IXlColumn column = sheet.CreateColumn())
    {
        column.WidthInPixels = 300;
    }

    // Create a hyperlink to a cell in the current workbook.
    using (IXlRow row = sheet.CreateRow())
    {
        using(IXlCell cell = row.CreateCell())
        {
            cell.Value = "Local link";
            cell.Formatting = XlCellFormatting.Hyperlink;
            XlHyperlink hyperlink = new XlHyperlink();
            hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
            hyperlink.TargetUri = "#Sheet1!C5";
            sheet.Hyperlinks.Add(hyperlink);
        }
    }

    // Create a hyperlink to a cell located in the external workbook.
    using (IXlRow row = sheet.CreateRow())
    {
        using(IXlCell cell = row.CreateCell())
        {
            cell.Value = "External file link";
            cell.Formatting = XlCellFormatting.Hyperlink;
            XlHyperlink hyperlink = new XlHyperlink();
            hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
            hyperlink.TargetUri = "linked.xlsx#Sheet1!C5";
            sheet.Hyperlinks.Add(hyperlink);
        }
    }

    // Create a hyperlink to a web page.
    using (IXlRow row = sheet.CreateRow())
    {
        using(IXlCell cell = row.CreateCell())
        {
            cell.Value = "External URI";
            cell.Formatting = XlCellFormatting.Hyperlink;
            XlHyperlink hyperlink = new XlHyperlink();
            hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
            hyperlink.TargetUri = "https://www.devexpress.com/";
            sheet.Hyperlinks.Add(hyperlink);
        }
    }
}