Saving macro enabled workbook gCrash excel

Oct 15, 2015 at 12:23 PM
Hello,
I am facing a strange error while trying to save a workbook.
For work reasons, I have to open a template , compile some cell, and then save a copy with the added info.
I tried this code:
String patOfStartingFile = @"somepath\a_template.xlsm";
        String patOfResultingFile = @"somepath\newfile " + .id+ ".xlsm";

        File.Copy(patOfStartingFile, patOfResultingFile);

        // Load the template into a NPOI workbook
        XSSFWorkbook wb = new XSSFWorkbook(patOfResultingFile);

        //calling writing methods to insert data into the workbook
        this.writesomething(something, wb);

        //saving results into a new file.
        FileStream xfile = new FileStream(patOfResultingFile, FileMode.Open, System.IO.FileAccess.Write);
        wb.Write(xfile);
        wb.Close();
        xfile.Close();
The code works fine , but if I try to open the generated file with Excel(2013), it crashes.
I also tried to not set any cell ( only open, copy and save) and the problem occurs again.
Somehow Excel fails to open(it does not even show the empty workbook, crashes at the opening), and I really do not get why.

I tried also this method of saving the result:
XSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(patOfResultingFile, FileMode.Open, FileAccess.Read))
        {
            hssfwb = new XSSFWorkbook(file);
            file.Close();
        }

        this.writesamedata(something, hssfwb);           

        using (FileStream file = new FileStream(patOfResultingFile, FileMode.Open, FileAccess.Write))
        {
            hssfwb.Write(file);
            file.Close();
        }
And with this method Excel opens, and ask to recover the file content (failing) - which is even more odd.
I really do not have a clue, somebody knows something about it?
Coordinator
Dec 1, 2015 at 12:12 AM
NPOI doesn't support macro-enabled Excel file (xlsm). I'm afraid the macro part is lost while saving via NPOI.