Problematic corruption of .xlsx files with NPOI - Excel cannot open the file 'file.xlsx" because the file format or file extension is not valid

Dec 12, 2016 at 3:55 PM
Edited Dec 12, 2016 at 3:58 PM
When reading or modifying some user-created .xlsx files, I get the following error message:
We found a problem with some content in 'test.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
Clicking Yes gets me another message:
Excel cannot open the file 'test.xlsx' 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 have no issues creating a new .xlsx file with the following code:
    string newPath = @"C:\MyPath\test.xlsx";
    using (FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write))
        IWorkbook wb = new XSSFWorkbook();
        ISheet s = wb.GetSheetAt(0);
        IRow r = s.CreateRow(0);
        ICell c = r.GetCell(0);
That works fine.

Even opening one of the problem child .xlsx files, setting it to an IWorkbook and writing it back to the file works:
    string newPath = @"C:\MyPath\test.xlsx";

    using (FileStream fs = new FileStream(newPath, FileMode.Open, FileAccess.ReadWrite))
        IWorkbook wb = new XSSFWorkbook(fs);
However, after running through code that reads from it, gets ISheets, IRows, ICells, etc.... it corrupts the .xlsx file. Even though I specifically removed anything that modifies the workbook. No Creates, Sets, Styles, etc. with NPOI.

I can't really include my code because it would just be confusing, but for the sake of completeness I'm really only using the following types and functions from NPOI during this test:
So one of those causes corruption. I would like to eventually set values again and get it working like I have for .xls.

Has anyone experienced this? What are some NPOI functions that could cause corruption? Any input would be appreciated.

Using NPOI 2.2.1.
Dec 12, 2016 at 6:03 PM
I figured it out. It didn't like that I wrote the changed IWorkbook to the same file. Creating a new file solved this issue. Also, I believe if you do want to overwrite it, FileAccess.ReadWrite also seemed to work for the FileStream.
Marked as answer by justiceorjustus on 12/12/2016 at 11:03 AM