officefileapi-115519-excel-export-library-cells-how-to-add-a-hyperlink-to-a-cell.md
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:
Initialize a new instance of the XlHyperlink class using the default XlHyperlink constructor.
Specify the cell or cell range to which the hyperlink should be attached by using the XlHyperlink.Reference property.
Use the XlHyperlinkBase.TargetUri property to specify the hyperlink destination. The following table lists possible locations to which hyperlinks can refer.
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.
// 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);
}
}
}
' 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