首页 > 解决方案 > EPPluse复杂超链接崩溃excel文件

问题描述

我正在使用 eppluse 库在 C# 中创建报告。
报告包含数百万个网址,我必须将其显示为超链接/可链接链接。
目前,一切正常,但某些 URL 破坏了我的 Reports.xlsx 文件。 Excel中的错误捕捉

代码示例:

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

标签: c#hyperlinkepplus

解决方案


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

推荐阅读