Chris
Chris

Reputation: 729

File corrupted simply by opening and saving with no changes

Working on some code to update a spreadsheet but I noticed the isolated act of opening the spreadsheet and saving it corrupts it, resulting in the message "We found a problem with some content in''. Do you want us to try to recover as much as we can?"

This can be replicated by the following code

FileInfo excelFile = new FileInfo("mysheet.xlsm");
using (ExcelPackage excel = new ExcelPackage(excelFile))
{
    excel.Save();
}

The resulting file also shrinks from around 3MB to 2MB in the process.

Seems like this should be pretty straight forward but I must be missing something crucial.

Upvotes: 0

Views: 672

Answers (2)

Chris
Chris

Reputation: 729

I couldn't get to the bottom of the problem and switched to the Interop library.

Upvotes: 2

Pete
Pete

Reputation: 1867

DISCAIMER: This isn't an answer, but I want to post some code

I've tried to reproduce your problem, but with no luck. Steps as follows:

I've created a spreadsheet manually in Excel 2010. The text in cell A1 of Sheet1 simply reads "Hello". I've also added a code module in the macro editor (Alt-F11) with the following code:

Option Explicit

Sub Foo()

    MsgBox "Hello World", vbOKOnly, "Foo!"

End Sub

I then saved this as a macro-enabled spreadsheet (C:\Temp\Book1.xlsm)

In Visual Studio I then created a C# console app and added the EPPlus package from NuGet. The main method looks like this:

static void Main(string[] args)
{
    using (var pck = new ExcelPackage(new FileInfo(@"c:\temp\Book1.xlsm")))
    {
        pck.Save();
    }
}

When I debug this it executes without issue.

Have you checked permissions on the folder you're saving to and forced a refresh of your EPPlus package?

Upvotes: 1

Related Questions