-
Notifications
You must be signed in to change notification settings - Fork 293
Hyperlinks
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;
To style the hyperlinks you can create the named style for the Hyperlinks.
var namedStyle = package.Workbook.Styles.CreateNamedStyle("HyperLink");
namedStyle.BuildInId = 8; //This is the id for the build in hyper link style.
var namedStyle = package.Workbook.Styles.CreateNamedStyle("Followed HyperLink");
namedStyle.BuildInId = 9; //This is the id for the build in followed hyper link style.
To set the style for the hyperlink, set the StyleName
of the cells to the created named style.
worksheet.Cells["A1:A3"].StyleName = "HyperLink";
To set the style for the hyperlink, set the StyleName
of the cells to the created named style.
worksheet.Cells["A1:A3"].StyleName = "HyperLink";
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;
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
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles