Search code examples
c#hyperlinkepplus

EPPluse complex hyperlink crashing excel file


I am using eppluse library for creating reports in c#.
Reports contain millions of urls that I have to show as hyperlink / clicable links.
Currently, everything is working but some URLs is breaking my Reports.xlsx file. Error Snap in excel

Code sample:

for (var j = 0; j < values.Length; j++)
{
    // Format values before printing
    object cellValue = ParseString(values[j].Replace("\"", ""));

    // Check for urls and conver to hyperlinks
    if (cellValue.GetType().Name.ToLower() == "uri")
    {
        worksheet.Cells[i + 1, j + 1].Hyperlink = new ExcelHyperLink(Uri.EscapeUriString(cellValue.ToString().Replace("[", "%5B").Replace("]", "%5D"))) { Display = "Link" };
        worksheet.Cells[i + 1, j + 1].StyleName = hyperLinkStyle.Name;
    }
    else
    {
        worksheet.Cells[i + 1, j + 1].Value = cellValue;
    }
}

Solution

  • Thanks for help following solution worked at the moment.
    I am not sure how long it going works, But currently working smoothly.

    string url = Uri.EscapeUriString(cellValue.ToString().Replace("[", "%5B").Replace("]", "%5D"));
            if (cellValue.ToString().Length < 256)
            {
                worksheet.Cells[i + 1, j + 1].Formula = string.Format("HYPERLINK(\"{0}\",\"{1}\")", url, "Link");
            }
            else
            {
                worksheet.Cells[i + 1, j + 1].Hyperlink = new ExcelHyperLink(url) { Display = "Link" };
            }