gpt4 book ai didi

excel - 当超过 65,530 行时,EPPlus 损坏的 Excel 文件

转载 作者:行者123 更新时间:2023-12-04 21:47:02 25 4
gpt4 key购买 nike

当超过 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;
}

最佳答案

使用“.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;

关于excel - 当超过 65,530 行时,EPPlus 损坏的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62647859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com