开发者

C# Epplus save & saveas error

开发者 https://www.devze.com 2023-03-26 04:02 出处:网络
if (!File.Exists(this.savePath.FullName + \"\\\\\" + value + \".xls开发者_JAVA技巧x\")) { using ( ExcelPackage exp = new ExcelPackage(finfo))
            if (!File.Exists(this.savePath.FullName + "\\" + value + ".xls开发者_JAVA技巧x"))
            {
                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();

                }
            }

So I am trying to use EPPlus to save to a specific folder thats obtained from the user. However although it saves it just fine in the first using instance, when I try to save or save as it simply throws out an error.

If I use the original file as a template(as I have below) and simply use the first part again it works fine. I have no idea why the save doesnt' work. I've tried to saveAs to a different location but this causes the same error.

If you have any idea please help me.

~edit Here is the error Error saving file C:\Documents and Settings\xxx\Desktop\Testing Andyxxxxxxxx\2481.xlsx

~edit Sorry for all the edits, I'm new to this It's an InvalidOperationException (unhandled)


I found the problem, you need to save the file before you try to dispose the worksheet, and in your case you need to ADD a new worksheet before reference it.


Try to update the Epplus dll to the latest version. Currently it is 4.0.5 and could be downloaded from here:

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

that helped me today.


I realize this is an old question, but I just ran into this with XLSX exported from a popular reports program. The XLSX references styles that aren't being saved. I fixed this with:

                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;
                    }
                }

This problem could happen for different reasons, but likely is a problem with the XLSX file.


I was getting this InvalidOperation as well with EPPlus 4.5.3.2. For me, the problem was that I was trying to call SaveAs() after the end of the Worksheet using block, even though the ExcelPackage was not yet to the end of its using block. So, make sure you are not disposing the inner worksheet(s) before saving the package.

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);
}


I was getting this InvalidOperation as well with EPPlus 4.5.3.2. For me, the problem was that in my excel template file, there is a image in it. Therefore, Azure Linux App Service throw an exception that:

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()

I found the solution in here: https://github.com/dotnet/core/issues/2746#issuecomment-595980412

Just insert the code below on "Startup command" changing the dll name.

enter image description here

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

0

精彩评论

暂无评论...
验证码 换一张
取 消