How To Create A Hyperlink Inside A Cell Or Image In Excel Sheet Using EPPlus .Net Application (C#) - Part Six

First EPPlus library can be downloaded from : http://epplus.codeplex.com/ & add this library to your Visual Studio solution or you can download my source code.

What is Hyperlink
  • A Hyperlink is a link inside a document that open a another destination object when users click on it. Here destination object means any web page, any files, emails address or any program. The Hyperlink itself can be text or a picture.
How to create Hyperlink in excel sheet using EPPlus Library. There are four ways to create Hyperlink in excel sheet.
  • Option 1 - Using EPPlus Hyperlink property of ExcelRange class.
  • Option 2 - Using AddPicture() method of ExcelDrawings class.
  • Option 3 - Using Excel Hyperlink Function in the formula property of ExcelRange class. (We will be discuss on this topic in Part-7 of this video series)
  • Option 4 - Using VBA (Visual Basic for Application) Code. It is a macro enable excel sheet. (We will discuss on this topic in my upcoming video series "Excel VBA code development using EPPlus Library")
Note

fFrst two options of Hyperlink accept relative and absolute URI .

What is Relative & Absolute URI (Uniform Resource Identifier)?

A URI is a compact sequence of characters that identifies an abstract or physical resource. A URI can be further classified as a locator, a name, or both. The term 'UniformResource Locator(URL) refers to the subset of URI.

The absolute URL contains all the information necessary to locate a resource.

An absolute URL format: scheme://server/path/resource
Example : https://everyday-be-coding.blogspot.in/p/epplus-library-part-6.html

A relative URL typically consists only of the path, and optionally, the resource, but no scheme or server.

Using EPPlus Hyperlink Property
  • ExcelRange Rng = wsSheet1.Cells["B6"];
  • Rng.Hyperlink = new Uri("https://www.google.com", UriKind.Absolute);
  • Rng.Value = "Go to GOOGLE";
Here wsSheet1 is the object of ExcelWorkSheet class. It hold the "B6" cell. Uri() is the constructor of class Uri & accept the string value URL as a parameter. It assign to the Uri class object, here object is Hyperlink property of ExcelRange class. That means the type of Hyperlink property is Uri class. After that we set value of "B6" cell by using Rng.Value property.

Hyperlink works with:
  • To another cell of existing sheet.
  • To different sheet within same excel file.
  • To any local file.
  • To any remote server file.
  • To link with email address.
To another cell of existing sheet:
  • ExcelRange Rng = wsSheet1.Cells["B7"];
  • Rng.Hyperlink = new Uri("#'Sheet1'!B2", UriKind.Relative);
  • Rng.Value = "Go to Cell B2";

In this above example #'<Sheet Name>'!<Cell address> is the link location & here# keyword specify the sheet name & ! sign used for relation operator with cell B2. If your excel file has multiple sheets, then you can use a specific sheet name. UriKind is a enum for specific addressing.

To different sheet within same excel file,

  • ExcelRange Rng = wsSheet1.Cells["B8"];
  • Rng.Hyperlink = new Uri("#'Sheet2'!B1", UriKind.Relative);
  • Rng.Value = "Go to B1 in Sheet2";

To any local file,

  • ExcelRange Rng = wsSheet1.Cells["B9"];
  • Rng.Hyperlink = new Uri(@"D:\sample.xlsx");
  • Rng.Value = "D:\\sample.xlsx";

*In this above example @"D:\sample.xlsx" is the local file path.

To any remote server file,

  • ExcelRange Rng = wsSheet1.Cells["B10"];
  • Rng.Hyperlink = new Uri("https://goo.gl/gOa0wm", UriKind.Absolute);
  • Rng.Value = "https://goo.gl/gOa0wm";
To link with email address,
Using AddPicture() method of ExcelDrawings class,
  • Image img = Image.FromFile(@"D:\sample.png");
  • ExcelPicture pic = wsSheet1.Drawings.AddPicture("EDBC", img, new Uri("https://everyday-be-coding.blogspot.in/", UriKind.Absolute));
  • Pic.SetPosition(14,0,1,0);
  • Pic.SetSize(148,26);
Above example was already discussed in Part-5 (EPPlus library) of this video series. In this example 3rd parameter's of AddPicture("Pic_Name", img, new Uri("https://everyday-be-coding.blogspot.in/")) method is URI constructor. By using this method we can attach a hyperlink on image file.

Output in Excel Sheet




Source code
  1. using System;  
  2. using OfficeOpenXml;  
  3. using System.IO;  
  4. using System.Drawing;  
  5. using OfficeOpenXml.Style;  
  6. using OfficeOpenXml.Style.XmlAccess;  
  7. namespace DemoEpplus {  
  8.     class Program {  
  9.         static void Main(string[] args) {  
  10.             ExcelPackage ExcelPkg = new ExcelPackage();  
  11.             ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");  
  12.             ExcelWorksheet wsSheet2 = ExcelPkg.Workbook.Worksheets.Add("Sheet2");  
  13.             using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {  
  14.                 Rng.Value = "Everyday Be Coding - Excel HYPERLINK using EPPlus .Net Library";  
  15.                 Rng.Style.Font.Size = 16;  
  16.                 Rng.Style.Font.Bold = true;  
  17.                 Rng.Style.Font.Italic = true;  
  18.                 Rng.Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  19.                 Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  20.                 Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  21.                 Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  22.             }  
  23.             //SET HYPERLINK STYLE-----------------------  
  24.             string StyleName = "HyperStyle";  
  25.             ExcelNamedStyleXml HyperStyle = wsSheet1.Workbook.Styles.CreateNamedStyle(StyleName);  
  26.             HyperStyle.Style.Font.UnderLine = true;  
  27.             HyperStyle.Style.Font.Size = 12;  
  28.             HyperStyle.Style.Font.Color.SetColor(Color.Blue);  
  29.             //------CREATE HYPERLINK USING EPPLUS HYPERLINK PROPERTY--------------  
  30.             using(ExcelRange Rng = wsSheet1.Cells[4, 1, 4, 1]) {  
  31.                 Rng.Value = "Option-1:";  
  32.                 Rng.Style.Font.Size = 13;  
  33.                 Rng.Style.Font.Bold = true;  
  34.                 Rng.Style.Font.Color.SetColor(Color.Red);  
  35.             }  
  36.             using(ExcelRange Rng = wsSheet1.Cells[4, 2, 4, 2]) {  
  37.                 Rng.Value = "Using EPPlus Hyperlink property of ExcelRange class";  
  38.                 Rng.Style.Font.Size = 13;  
  39.             }  
  40.             //------HYPERLINK to a website.  
  41.             using(ExcelRange Rng = wsSheet1.Cells[6, 2, 6, 2]) {  
  42.                 Rng.Hyperlink = new Uri("https://www.google.com", UriKind.Absolute);  
  43.                 Rng.Value = "Go to GOOGLE";  
  44.                 Rng.StyleName = StyleName;  
  45.             }  
  46.             //------HYPERLINK to a cell within same sheet.  
  47.             using(ExcelRange Rng = wsSheet1.Cells[7, 2, 7, 2]) {  
  48.                 Rng.Hyperlink = new Uri("#'Sheet1'!B2", UriKind.Relative);  
  49.                 Rng.Value = "Go to Cell B2";  
  50.                 Rng.StyleName = StyleName;  
  51.             }  
  52.             //------HYPERLINK to another sheet within same excel file.  
  53.             using(ExcelRange Rng = wsSheet1.Cells[8, 2, 8, 2]) {  
  54.                 Rng.Hyperlink = new Uri("#'Sheet2'!B1", UriKind.Relative);  
  55.                 Rng.Value = "Go to Cell B1 in Sheet2";  
  56.                 Rng.StyleName = StyleName;  
  57.             }  
  58.             //------HYPERLINK with any local file.  
  59.             using(ExcelRange Rng = wsSheet1.Cells[9, 2, 9, 2]) {  
  60.                 Rng.Hyperlink = new Uri(@ "D:\sample.xlsx");  
  61.                 Rng.Value = "D:\\sample.xlsx";  
  62.                 Rng.StyleName = StyleName;  
  63.             }  
  64.             //------HYPERLINK with any remote server file.  
  65.             using(ExcelRange Rng = wsSheet1.Cells[10, 2, 10, 2]) {  
  66.                 Rng.Hyperlink = new Uri("https://goo.gl/gOa0wm", UriKind.Absolute);  
  67.                 Rng.Value = "https://goo.gl/gOa0wm";  
  68.                 Rng.StyleName = StyleName;  
  69.             }  
  70.             using(ExcelRange Rng = wsSheet1.Cells[11, 2, 11, 2]) {  
  71.                 Rng.Hyperlink = new Uri("mailto:[email protected]", UriKind.Absolute);  
  72.                 Rng.Value = "[email protected]";  
  73.                 Rng.StyleName = StyleName;  
  74.             }  
  75.             //------CREATE HYPERLINK USING EPPLUS wsSheet1.Drawings.AddPicture() Method  
  76.             using(ExcelRange Rng = wsSheet1.Cells[13, 1, 13, 1]) {  
  77.                 Rng.Value = "Option-2:";  
  78.                 Rng.Style.Font.Size = 13;  
  79.                 Rng.Style.Font.Bold = true;  
  80.                 Rng.Style.Font.Color.SetColor(Color.Red);  
  81.             }  
  82.             using(ExcelRange Rng = wsSheet1.Cells[13, 2, 13, 2]) {  
  83.                 Rng.Value = "Using Epplus AddPicture() Method of ExcelDrawings class";  
  84.                 Rng.Style.Font.Size = 13;  
  85.             }  
  86.             Image img = Image.FromFile(@ "D:\EverydayBeCoding.png");  
  87.             ExcelPicture pic = wsSheet1.Drawings.AddPicture("Picture_Name", img, new Uri("https://everyday-be-coding.blogspot.in/", UriKind.Absolute));  
  88.             //ExcelPicture img = wsSheet1.Drawings.AddPicture("Picture_Name", new FileInfo(@"D:\EverydayBeCoding.png"), new Uri("https://everyday-be-coding.blogspot.in/", UriKind.Absolute ));  
  89.             pic.SetPosition(14, 0, 1, 0);  
  90.             pic.SetSize(148, 26);  
  91.             wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();  
  92.             ExcelPkg.SaveAs(new FileInfo(@ "D:\Hyperlink.xlsx"));  
  93.         }  
  94.     }  
  95. }  
Now build & execute this code. File is (Hyperlink.xlsx) store on D: drive of computer.

Thank you for reading this article. Please subscribe to my YouTube Channel & don't forget to like and share.

YouTubehttps://goo.gl/rt4tHH
Facebookhttps://goo.gl/m2skDb
Twitterhttps://goo.gl/nUwGnf