Read/Write XLSM using POI

Jul 26, 2016 at 9:23 AM
Edited Jul 26, 2016 at 10:09 AM
Hi guys,

I'm using POI in my c# code for few weeks, and i've found and issue i don't manage to solve: Xlsm
I've to open an xlsm template, edit it and save it. The template contains Macro, but i just add data into the xlsm file.
Actually, I think that read and Edit work find (thanks to debugging in vs), the issue is when I try to write...
The code is working, there is no error, but when I Try to open it with Excel, it crashes..
for example, even this crashes..

FileStream temp = new FileStream(xlsm_file, FileMode.Open, FileAccess.ReadWrite);
XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.Create(temp);
XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0);
workbook.SetSheetName(0, sheetname.Substring(0, Math.Min(sheetname.Length, 30)));
IRow header_Row = sheet.GetRow(0);
temp.Close();
FileStream toWrite = new FileStream(xlsm_file, FileMode.Open, FileAccess.ReadWrite);
workbook.Write(toWrite);
toWrite.Close();

Is anyone having an Idea?
Thanks !