【问题标题】:C# Epplus save & saveas errorC# Epplus 保存和另存为错误
【发布时间】:2020-04-01 10:31:43
【问题描述】:
            if (!File.Exists(this.savePath.FullName + "\\" + value + ".xlsx"))
            {
                using ( ExcelPackage exp = new ExcelPackage(finfo))
                {
                    //ExcelPackage exps= new ExcelPackage(pather);
                    ExcelWorksheet exlss = exp.Workbook.Worksheets[timing];
                    exlss.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.Medium9);
                    exp.SaveAs(existing);

                }
            }
            else if (File.Exists(this.savePath.FullName + "\\" + value + ".xlsx")) {
                timing = "2011";
                using (ExcelPackage exp = new ExcelPackage(existing))
                {

                    //ExcelPackage exps= new ExcelPackage(pather);
                    ExcelWorksheet exlss = exp.Workbook.Worksheets[timing];
                    exlss.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.Medium9);
                    exp.Save();

                }
            }

所以我尝试使用 EPPlus 保存到从用户那里获得的特定文件夹。但是,尽管它在第一个使用实例中保存得很好,但当我尝试保存或保存时,它只会抛出错误。

如果我使用原始文件作为模板(如下所示)并再次使用第一部分,它就可以正常工作。我不知道为什么保存不起作用。我试图 saveAs 到不同的位置,但这会导致同样的错误。

如果您有任何想法,请帮助我。

~edit 这是错误 保存文件 C:\Documents and Settings\xxx\Desktop\Testing Andyxxxxxxxx\2481.xlsx 时出错

~edit 抱歉所有的编辑,我是新手 这是一个 InvalidOperationException(未处理)

【问题讨论】:

  • 您也可以发布错误吗?
  • 保存文件时出错 C:\Documents and Settings***\Desktop\Testing Andy ****\2481.xlsx 抱歉
  • 和内部异常?
  • 我对 NuGet 的 4.0.5 版本有同样的问题。错误是:保存文件 D:\.xlsx 时出错,内部消息异常是“对象引用未设置为对象的实例”。并且堆栈跟踪位于 CellsStoreEnumerator`1.Init() at OfficeOpenXml.ExcelStyles.RemoveUnusedStyles() at OfficeOpenXml.ExcelStyles.UpdateXml() at OfficeOpenXml.ExcelWorkbook.Save() at OfficeOpenXml.ExcelPackage.Save()

标签: c# excel


【解决方案1】:

我发现了问题,您需要在尝试处理工作表之前保存文件,在您的情况下,您需要在引用之前添加一个新工作表。

【讨论】:

  • 保存没有帮助,4.5.2.1 版中仍然存在问题
【解决方案2】:

尝试将 Epplus dll 更新到最新版本。目前是 4.0.5,可以从这里下载:

http://epplus.codeplex.com/downloads/get/813458

今天对我有帮助。

【讨论】:

  • 这应该是一条评论
【解决方案3】:

我意识到这是一个老问题,但我只是在使用从流行的报告程序导出的 XLSX 时遇到了这个问题。 XLSX 引用了未保存的样式。我用以下方法解决了这个问题:

                for (int rowIndex = wks.Dimension.Start.Row; rowIndex <= wks.Dimension.End.Row; rowIndex++)
                {
                    var row = wks.Row(rowIndex);
                    try
                    {
                        var s = row.Style;
                    }
                    catch (Exception)
                    {
                        row.StyleID = 0;
                    }
                }

发生此问题的原因可能不同,但很可能是 XLSX 文件的问题。

【讨论】:

    【解决方案4】:

    我在 EPPlus 4.5.3.2 中也得到了这个 InvalidOperation。对我来说,问题是我试图在 Worksheet using 块结束后调用 SaveAs(),即使 ExcelPackage 尚未到其 using 块的末尾。因此,请确保在保存包之前没有处理内部工作表。

    using (var excelPackage = new ExcelPackage())
    {
        adapter.SelectCommand.CommandTimeout = commandTimeout;
        adapter.Fill(table);
    
        // If you wrap this in a using block which terminates before Save()/SaveAs()
        // it will throw an InvalidOperationException
        ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add(table.TableName);
    
        worksheet.Cells["A1"].LoadFromDataTable(table, true);
    
        //Format the header
        using (ExcelRange range = worksheet.Cells[1, table.Columns.Count]) // all columns
        {
            range.Style.Font.Bold = true;
            ... other stuff
        }
    
        var newFile = new FileInfo(path);
        excelPackage.SaveAs(newFile);
    }
    

    【讨论】:

      【解决方案5】:

      我在 EPPlus 4.5.3.2 中也得到了这个 InvalidOperation。对我来说,问题是在我的 excel 模板文件中,里面有一张图片。因此,Azure Linux App Service 会抛出一个异常:

      2020-04-01T02:01:29.144596907Z System.InvalidOperationException: Error saving file /home/site/wwwroot/wwwroot/export-files/YeuCauVatTu_20200401090128.xlsx
      2020-04-01T02:01:29.144612407Z  ---> System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
      2020-04-01T02:01:29.145214910Z  ---> System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory
      2020-04-01T02:01:29.145238810Z    at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
      2020-04-01T02:01:29.145245610Z    at System.Drawing.SafeNativeMethods.Gdip..cctor()
      

      我在这里找到了解决方案:https://github.com/dotnet/core/issues/2746#issuecomment-595980412

      只需在“启动命令”中插入以下代码即可更改 dll 名称。

      enter image description here

      apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev && dotnet YourWebSite.dll

      【讨论】:

        猜你喜欢
        • 2012-11-11
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 1970-01-01
        相关资源
        最近更新 更多