How to create a link inside a cell using EPPlus

C#ExcelEpplus

C# Problem Overview


I am trying to figure out how to write a Hyperlink inside a cell using EPPlus instead of the cell containing the link text. I need it to be recognized as a link and be clickable.

Any help is appreciated.

C# Solutions


Solution 1 - C#

This is the other way to do:

var cell = sheet.Cells["A1"];
cell.Hyperlink = new Uri("http://www.google.com");
cell.Value = "Click me!";

I have tested. It works fine.

Solution 2 - C#

The below code worked fine with me.

string FileRootPath = "http://www.google.com";
_Worksheet.Cells[intCellNumber, 1].Formula = "HYPERLINK(\"" + FileRootPath + "\",\"" + DisplayText + "\")";

I hope this would help you.

Happy coding!!

Solution 3 - C#

There's a few ways to go about it:

  1. To use URI, then set a human readable name:

    var cell = sheet.Cells["A1"]; cell.Hyperlink = new Uri("https://www.google.com";); cell.Value = "Click me!";

  2. To use ExcelHyperLink and set a human readable name using object initializer :

    var cell = sheet.Cells["A1"]; cell.Hyperlink = new ExcelHyperLink("https://www.google.com";) { Display = "Click me!" };

  3. To use =Hyperlink() formula:

    var cell = sheet.Cells["A1"]; cell.Formula = string.Format("HYPERLINK({0},{1})", "https://www.google.com";, "Click me!"); cell.Calculate();

Solution 4 - C#

Based on provided answers and documentation I was able to create an extension method that also deals with proper hyperlink formatting. It creates a named style, if needed, and use that style for all subsequent hyperlinks:

public static void WriteHyperlink(this ExcelRange cell, string text, string url, bool excelHyperlink = false, bool underline = true)
{
    if (string.IsNullOrWhiteSpace(text))
        return;

    // trying to reuse hyperlink style if defined
    var workBook = cell.Worksheet.Workbook;
    string actualStyleName = underline ? HyperLinkStyleName : HyperLinkNoUnderlineStyleName;

    var hyperlinkStyle = workBook.Styles.NamedStyles.FirstOrDefault(s => s.Name == actualStyleName);
    if (hyperlinkStyle == null)
    {
        var namedStyle = workBook.Styles.CreateNamedStyle(actualStyleName);  
        namedStyle.Style.Font.UnderLine = underline;
        namedStyle.Style.Font.Color.SetColor(Color.Blue);
    }

    if (excelHyperlink)
        cell.Hyperlink = new ExcelHyperLink(url) { Display = text };
    else
    {
        cell.Hyperlink = new Uri(url);
        cell.Value = text;
        cell.StyleName = actualStyleName;
    }
}

Without the styling, the hyperlink will look just as regular text, if cell.Hyperlink = new Uri(url); is used without explicit styling (although the cursor will properly indicate that the text is actually a hyperlink text).

Solution 5 - C#

I don't know EPPlus, but in VBA (and I guess C# would use the same principle) you would use the following code:

Sub Test()

    ' place value into cell
    ActiveSheet.[A1] = 13

    ' create link and set its range property
    ActiveSheet.Hyperlinks.Add ActiveSheet.[A1], "http://www.google.at"

    ' use cell in a calculation
    ActiveSheet.[A2].Formula = "=A1+2"
    
End Sub

Hyperlinks are objects having a range property, so while your cell value can be changed by overtyping, the link will remain. Edit the cell by a long mouse click

Hope this helps - good luck MikeD

Solution 6 - C#

If you are using EPPlus and want to create a link to another sheet within the same document, then this is the proper way to do this:

  var link = "Another Excel Sheet"; //Maximum length is 31 symbols
  using (var excel = new ExcelPackage())
  {
       var ws = excel.Workbook.Worksheets.Add("Test");
       ws.Cells[row, col].Hyperlink =
                new ExcelHyperLink((char)39 + link + (char)39 + "!A1", 
                "Name of another excel sheet could be more then 31 symbols");
  }

This is the proper way to create a link to another sheet within Excel document. Having using formula with HYPERLINK function, if a file is downloaded to the client, latest Excel version will raise security warnings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIEnumeratorView Question on Stackoverflow
Solution 1 - C#HanView Answer on Stackoverflow
Solution 2 - C#BellView Answer on Stackoverflow
Solution 3 - C#CardinView Answer on Stackoverflow
Solution 4 - C#Alexei - check CodidactView Answer on Stackoverflow
Solution 5 - C#MikeDView Answer on Stackoverflow
Solution 6 - C#SouXinView Answer on Stackoverflow