How to create a link inside a cell using EPPlus
I a开发者_如何学运维m 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;
}
}
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).
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");
}
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.
精彩评论