【问题标题】:EPPlus corrupt Excel file when having more than 65,530 rows当超过 65,530 行时,EPPlus 损坏的 Excel 文件
【发布时间】:2020-06-29 22:57:39
【问题描述】:

当超过 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 hyperlink epplus corrupt


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这是因为 Excel 将文件中唯一 URL 的数量限制为 65,530 个。您应该尝试将它们作为文本插入,而不是 url。

      有关可能的解决方案,请查看this answer

      【讨论】:

      • 哇!我将尝试提出不同的方法。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      相关资源
      最近更新 更多