programing

How to create a link inside a cell using EPPlus

yellowcard 2023. 4. 10. 21:31
반응형

How to create a link inside a cell using EPPlus

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.

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.

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!!

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();

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;
    }
}

스타일링을 사용하지 않으면 하이퍼링크는 일반 텍스트와 동일하게 표시됩니다.cell.Hyperlink = new Uri(url);는 명시적인 스타일링 없이 사용됩니다(커서는 텍스트가 실제로 하이퍼링크텍스트임을 올바르게 나타냅니다).

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

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");
  }

이는 Excel 문서 내의 다른 시트에 대한 링크를 작성하기 위한 적절한 방법입니다.수식을 사용하는 것HYPERLINK파일이 클라이언트에 다운로드되면 최신 Excel 버전에서 보안 경고가 발생합니다.

ReferenceURL : https://stackoverflow.com/questions/7639180/how-to-create-a-link-inside-a-cell-using-epplus

반응형