public void Write(Stream stream) { FlushSheets(); //Save the template var tmplFile = TempFile.CreateTempFile("poi-sxssf-template", ".xlsx"); try { var os = new FileStream(tmplFile.FullName, FileMode.Open, FileAccess.ReadWrite); try { XssfWorkbook.Write(os); } finally { os.Close(); } //Substitute the template entries with the generated sheet data files InjectData(tmplFile, stream); } finally { tmplFile.Delete(); if (File.Exists(tmplFile.FullName)) { throw new IOException("Could not delete temporary file after processing: " + tmplFile); } } }