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:
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!";
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!" };
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
'programing' 카테고리의 다른 글
병렬 리스트의 해당 값에 따라 리스트 정렬 (0) | 2023.04.10 |
---|---|
iPhone에서 사용자가 푸시 알림을 활성화했는지 확인합니다. (0) | 2023.04.10 |
bash 스크립트에서 파일 이름 확장자를 확인하는 방법 (0) | 2023.04.10 |
어레이의 각 루프에 대해를 사용하려면 어떻게 해야 합니까? (0) | 2023.04.10 |
Swift 포함기존 UIKit 응용 프로그램의 UI 보기 (0) | 2023.04.10 |