EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Unable to open workbook after writing with EEPlus

Open rdhasse opened this issue 4 years ago • 6 comments

Using version 5.5.2, after writing to an existing workbook we are unable to open it with error message:

"Excel cannot open the file because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file".

I debugged a bit and found that:

  1. This only happens when macros are enabled (*.xlsm file). If I save as *.xlsx the issue does not occur.
  2. I deleted all Named Ranges from the *.xlsm file and the issue still happens.
  3. I was able to change the corrupted file to *.zip and extract the contents.

Let me know if there is anything I can do to provide further information. I don't think we would be able to send the file.

Thanks,

Randall

rdhasse avatar Jan 26 '21 18:01 rdhasse

Can you attach the existing xlsm file you are using, so we can reproduce the issue?

JanKallman avatar Jan 27 '21 07:01 JanKallman

I created a brand new macro-enabled Excel file and the issue occurred with that. I zipped up and attached that file. We are simply creating a new ExcelWorksheet in that workbook, and then writing some data to that worksheet.

ExcelWorksheet ws = wb.Worksheets.Add(name); //then write some data to the new worksheet

test_macro_enabled_workbook.zip

rdhasse avatar Jan 27 '21 18:01 rdhasse

In case it helps, I also attached the file after the write that has the issue.

test_macro_enabled_workbook_with_issue.zip

rdhasse avatar Jan 27 '21 18:01 rdhasse

This workbook does not have a vbaProject.bin which causes the issue. If you add this row the issue will go away... package.Workbook.CreateVBAProject(); If no vbaProject.bin is found EPPlus saves the package as non macro enabled (xlsx), which causes the issue. I will look into the handling of this, as in this case it gives unexpected results.

JanKallman avatar Jan 28 '21 07:01 JanKallman

Thanks. One question though. If we call

package.Workbook.CreateVBAProject();

Will any VBA macros that existed in the original workbook be lost? If so, I think we would prefer to wait for a fix.

rdhasse avatar Jan 28 '21 12:01 rdhasse

package.Workbook.CreateVBAProject(); will fail if there is a vba project in the file. You can check that by p.Workbook.VbaProject!=null

JanKallman avatar Jan 28 '21 14:01 JanKallman

Closed due to inactivity.

OssianEPPlus avatar Feb 08 '24 10:02 OssianEPPlus