i'm struggling with adding hyperlinks to another list to my generated excel file. I tried it like this:
ws.Cells[1, 1].Formula="HYPERLINK(\"[#]'sheetName'!R4C1\";\"linktext\")"
ws.Cells[1, 1].Formula="HYPERLINK(\"#'sheetName'!R4C1\";\"linktext\")"
ws.Cells[1, 1].FormulaR1C1="HYPERLINK(\"[#]'sheetName'!R4C1\";\"linktext\")"
ws.Cells[1, 1].FormulaR1C1="HYPERLINK(\"#'sheetName'!R4C1\";\"linktext\")"
After opening generated excel file in excel 365 (full offline application) i just get message that there are errors in the file and all the cells where I used this hyperlink formula are empty.
The error message is:
we found a problem with some content in FILENAME do you want us to try to recover as much as we can
How to make it work?
Also when i put same formula in cell manually it works.
Complete code:
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelHyperlinkTest
{
class Program
{
static void Main(string[] args)
{
MemoryStream excelStream = new MemoryStream();
ExcelPackage excelFile = new ExcelPackage(excelStream);
ExcelWorksheet wsSrc = excelFile.Workbook.Worksheets.Add("src");
ExcelWorksheet wsTgt = excelFile.Workbook.Worksheets.Add("tgt");
wsSrc.Cells[1, 1].Formula = string.Format("HYPERLINK(\"#'{0}'!R{1}C{2}\";\"{3}\")", "tgt", 2, 5, "test"); //FormulaR1C1
excelFile.Save();
excelStream.Position = 0;
using (FileStream file = new FileStream("c:\\linkTest.xlsx", FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[excelStream.Length];
excelStream.Read(bytes, 0, (int)excelStream.Length);
file.Write(bytes, 0, bytes.Length);
excelStream.Close();
}
}
}
}
This works
wsSrc.Cells[1, 6].Value = "test3";
Uri url = new Uri("#'tgt'!B5", UriKind.Relative);
wsSrc.Cells[1, 6].Hyperlink = url;
If the sheet exist in the same excel file and any one using EPPlus library, then following code might help them:
int rowCount=<your specific row>
ws.Cells[rowCount, 6].Hyperlink = new ExcelHyperLink((char)39 + "Name of your sheet" +
(char)39 + "!A1(specific cell on that sheet)", "Link text");
This will link to B5 in the sheet (sheetName) in a workbook (workbookPath) , relative to the current workbook and formats the link as a hyperlink
using (ExcelRange rng = xlsheetSummary.Cells[1, 1])
{
var namedStyle = xlsheetSummary.Workbook.Styles.NamedStyles.FirstOrDefault(o=> o.Name == "HyperLink");
namedStyle.Style.Font.UnderLine = true;
rng.StyleName = namedStyle.Name;
rng.Value = ws.Name;
Uri link = new Uri($"{workbookPath}#'{sheetName}'!B5", UriKind.Relative);
namedStyle.Style.Font.Color.SetColor(Color.Blue);
rng.Hyperlink = link;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.