c# - 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;
}
}
解决方案
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" };
}
推荐阅读
- asp.net-core - .net 核心 web api 中未收到 RestRequest 正文
- sql - SQL 无法在列中插入转换后的日期时间
- http - 如何使用http post请求在flutter中进行电话身份验证?
- linux - redhat 中的时区更改
- node.js - 在 CLI 中运行时,Jest 测试不起作用
- azure-cosmosdb - CosmosDB 能否成为简单的键/值存储
- python - 对 numpy 数组应用条件并删除高维元素
- anaconda - conda 使用错误的 python 版本安装 pkg
- pycharm - 【】范围内不能有变量?或者0-1变量不能加减?
- ruby-on-rails - 使用关注点的 Rails 自定义记录器不起作用