Hyperlinks
Hyperlinks in cells.¶
Hyperlinks can be added to cells using the ExcelRangeBase.SetHyperlink method or the ExcelRangeBase.Hyperlink property. You can set the hyperlink using the standard .NET Uri class or the extended ExcelHyperLink class.
For example:
worksheet.Cells["A1"].SetHyperlink(worksheet.Cells["G12"], "Link to cell G12..");
worksheet.Cells["A2"].Hyperlink = new Uri("https://epplussoftware.com", UriKind.Absolute);
var hyperLink = new ExcelHyperLink("https://samples.epplussoftware.com/HtmlExport/", UriKind.Absolute);
hyperLink.Display = "EPPlus HTML Export Sample Website...";
hyperLink.ToolTip = "Follow this link to the EPPlus HTML Export sample website";
worksheet.Cells["A3"].Hyperlink = hyperLink;
worksheet.Cells("A1").SetHyperlink(worksheet.Cells("G12"), "Link to cell G12..")
worksheet.Cells("A2").Hyperlink = New Uri("https://epplussoftware.com", UriKind.Absolute)
Dim hyperLink = New ExcelHyperLink("https://samples.epplussoftware.com/HtmlExport/", UriKind.Absolute)
hyperLink.Display = "EPPlus HTML Export Sample Website..."
hyperLink.ToolTip = "Follow this link to the EPPlus HTML Export sample website"
worksheet.Cells("A3").Hyperlink = hyperLink
To style the hyperlinks you can create the named style for the Hyperlinks.
To set the style for the hyperlink, set the StyleName of the cells to the created named style.
Drawings as Hyperlinks¶
To set the style for the hyperlink, set the StyleName of the cells to the created named style.
Drawings as Hyperlinks¶
If you want a hyperlink that is not connected to a cell, you can add a drawing with a hyperlink.
In this sample we add a shape that is positioned absolute, to keep its position when rows or columns are hidden:
//Add a drawing with a HyperLink to the statistics sheet.
//We add the hyperlink as a drawing here, as we don't want it to move when we expand and collapse rows.
var hyperLink =ws.Drawings.AddShape("HyperLink", eShapeStyle.Rect);
hyperLink.Hyperlink = new ExcelHyperLink("Statistics!A1", "Statistics");
hyperLink.SetPosition(13, 0, 9, 0);
hyperLink.SetSize(70, 30);
hyperLink.EditAs = eEditAs.Absolute;
hyperLink.Border.Fill.Style = eFillStyle.NoFill;
hyperLink.Fill.Style = eFillStyle.NoFill;
hyperLink.Text = "Statistics";
hyperLink.Font.UnderLine = eUnderLineType.Single;
hyperLink.Font.Fill.Color = Color.Blue;
'Add a drawing with a HyperLink to the statistics sheet.
'We add the hyperlink as a drawing here, as we don't want it to move when we expand and collapse rows.
Dim hyperLink = ws.Drawings.AddShape("HyperLink", eShapeStyle.Rect)
hyperLink.Hyperlink = New ExcelHyperLink("Statistics!A1", "Statistics")
hyperLink.SetPosition(13, 0, 9, 0)
hyperLink.SetSize(70, 30)
hyperLink.EditAs = eEditAs.Absolute
hyperLink.Border.Fill.Style = eFillStyle.NoFill
hyperLink.Fill.Style = eFillStyle.NoFill
hyperLink.Text = "Statistics"
hyperLink.Font.UnderLine = eUnderLineType.Single
hyperLink.Font.Fill.Color = Color.Blue
See also¶
More extensive examples are available in our sample projects:
Sample 1.6 and Sample 3.4 for C#
Sample 1.6 and Sample 3.4-VB for Visual Basic