EPPlus 在超过 65,530 行时损坏 Excel 文件
EPPlus corrupt Excel file when having more than 65,530 rows
当有超过 65,530 行的列具有超链接时,我 运行 遇到 EPPlus 问题。下面的示例配置为创建 65,530 行。使用此编号,它将正确创建 Excel 文件(未损坏)。一旦你 运行 它超过 65,530,就会创建 Excel 文件,但是当你打开它时,Excel 会报告它已损坏。有解决此问题的想法吗?
try
{
int maxRowsToCreate = 65530; //-- no errors will be generated
//int maxRowsToCreate = 65531; //-- error will be generated. The Excel file will be created but will give an error when trying to open it.
string report = string.Format("D:\temp\hypelinkIssue-{0}.xlsx", maxRowsToCreate.ToString());
if (File.Exists(report))
{
File.Delete(report);
}
using (ExcelPackage pck = new ExcelPackage(new System.IO.FileInfo(report)))
{
//Add the Content sheet
var ws = pck.Workbook.Worksheets.Add("Catalog");
ws.View.ShowGridLines = true;
var namedStyle = pck.Workbook.Styles.CreateNamedStyle("HyperLink"); //This one is language dependent
namedStyle.Style.Font.UnderLine = true;
namedStyle.Style.Font.Color.SetColor(Color.Blue);
ws.Column(1).Width = 100;
int rowIndex = 0;
for (int i = 0; i < maxRowsToCreate; i++)
{
rowIndex += 1;
string fullFilePath = string.Format("D:\temp\{0}", Path.GetRandomFileName());
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new Uri(string.Format(@"file:///{0}", fullFilePath));
ws.Cells[rowIndex, 1].Value = fullFilePath;
}
pck.Save();
}
System.Diagnostics.Process.Start(report);
}
catch (Exception ex)
{
throw ex;
}
这是因为 Excel 将文件中的唯一 URL 数量限制为 65,530。您应该尝试将它们作为文本插入,而不是 url.
有关可能的解决方案,请查看 。
使用“.Hyperlink”时出现问题。相反,如果我使用“.Formula”并用“=HYPERLINK”Excel 公式填充它,它就可以正常工作。使用这种方法,我能够创建具有唯一超链接的 250k 记录。我没有尝试超过 250k,但希望它能正常工作。
感谢您为我指明正确的方向。
/*
This only works with LESS than 65,530 hyperlinks
*/
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new OfficeOpenXml.ExcelHyperLink(fullFilePath, ExcelHyperLink.UriSchemeFile);
ws.Cells[rowIndex, 1].Value = fullFilePath;
/*
This works with more that 65,530 hyperlinks
*/
string cellFormula = string.Format("=HYPERLINK(\"{0}\")", filePath);
ws.Cells[rowIndex, 1].Formula = cellFormula;
当有超过 65,530 行的列具有超链接时,我 运行 遇到 EPPlus 问题。下面的示例配置为创建 65,530 行。使用此编号,它将正确创建 Excel 文件(未损坏)。一旦你 运行 它超过 65,530,就会创建 Excel 文件,但是当你打开它时,Excel 会报告它已损坏。有解决此问题的想法吗?
try
{
int maxRowsToCreate = 65530; //-- no errors will be generated
//int maxRowsToCreate = 65531; //-- error will be generated. The Excel file will be created but will give an error when trying to open it.
string report = string.Format("D:\temp\hypelinkIssue-{0}.xlsx", maxRowsToCreate.ToString());
if (File.Exists(report))
{
File.Delete(report);
}
using (ExcelPackage pck = new ExcelPackage(new System.IO.FileInfo(report)))
{
//Add the Content sheet
var ws = pck.Workbook.Worksheets.Add("Catalog");
ws.View.ShowGridLines = true;
var namedStyle = pck.Workbook.Styles.CreateNamedStyle("HyperLink"); //This one is language dependent
namedStyle.Style.Font.UnderLine = true;
namedStyle.Style.Font.Color.SetColor(Color.Blue);
ws.Column(1).Width = 100;
int rowIndex = 0;
for (int i = 0; i < maxRowsToCreate; i++)
{
rowIndex += 1;
string fullFilePath = string.Format("D:\temp\{0}", Path.GetRandomFileName());
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new Uri(string.Format(@"file:///{0}", fullFilePath));
ws.Cells[rowIndex, 1].Value = fullFilePath;
}
pck.Save();
}
System.Diagnostics.Process.Start(report);
}
catch (Exception ex)
{
throw ex;
}
这是因为 Excel 将文件中的唯一 URL 数量限制为 65,530。您应该尝试将它们作为文本插入,而不是 url.
有关可能的解决方案,请查看
使用“.Hyperlink”时出现问题。相反,如果我使用“.Formula”并用“=HYPERLINK”Excel 公式填充它,它就可以正常工作。使用这种方法,我能够创建具有唯一超链接的 250k 记录。我没有尝试超过 250k,但希望它能正常工作。
感谢您为我指明正确的方向。
/*
This only works with LESS than 65,530 hyperlinks
*/
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new OfficeOpenXml.ExcelHyperLink(fullFilePath, ExcelHyperLink.UriSchemeFile);
ws.Cells[rowIndex, 1].Value = fullFilePath;
/*
This works with more that 65,530 hyperlinks
*/
string cellFormula = string.Format("=HYPERLINK(\"{0}\")", filePath);
ws.Cells[rowIndex, 1].Formula = cellFormula;