Back to Devexpress

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

officefileapi-115519-excel-export-library-cells-how-to-add-a-hyperlink-to-a-cell.md

latest5.0 KB
Original Source

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

  • Sep 19, 2023
  • 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.

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

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

csharp
// 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);
        }
    }
}
vb
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
    Using column As IXlColumn = sheet.CreateColumn()
        column.WidthInPixels = 300
    End Using

    ' Create a hyperlink to a cell in the current workbook.
    Using row As IXlRow = sheet.CreateRow()
        Using cell As IXlCell = row.CreateCell()
            cell.Value = "Local link"
            cell.Formatting = XlCellFormatting.Hyperlink
            Dim hyperlink As New XlHyperlink()
            hyperlink.Reference = New XlCellRange(New XlCellPosition(cell.ColumnIndex, cell.RowIndex))
            hyperlink.TargetUri = "#Sheet1!C5"
            sheet.Hyperlinks.Add(hyperlink)
        End Using
    End Using

    ' Create a hyperlink to a cell located in the external workbook.
    Using row As IXlRow = sheet.CreateRow()
        Using cell As IXlCell = row.CreateCell()
            cell.Value = "External file link"
            cell.Formatting = XlCellFormatting.Hyperlink
            Dim hyperlink As New XlHyperlink()
            hyperlink.Reference = New XlCellRange(New XlCellPosition(cell.ColumnIndex, cell.RowIndex))
            hyperlink.TargetUri = "linked.xlsx#Sheet1!C5"
            sheet.Hyperlinks.Add(hyperlink)
        End Using
    End Using

    ' Create a hyperlink to a web page.
    Using row As IXlRow = sheet.CreateRow()
        Using cell As IXlCell = row.CreateCell()
            cell.Value = "External URI"
            cell.Formatting = XlCellFormatting.Hyperlink
            Dim hyperlink As New XlHyperlink()
            hyperlink.Reference = New XlCellRange(New XlCellPosition(cell.ColumnIndex, cell.RowIndex))
            hyperlink.TargetUri = "https://www.devexpress.com/"
            sheet.Hyperlinks.Add(hyperlink)
        End Using
    End Using
End Using