I'm evaluating EPPlus as a replacement for GemBox, but can't even save a valid Excel file. I have this minimal C# code:
In library:
public Stream GenerateReport(string templateFilePath)
{
using (var xls = new OfficeOpenXml.ExcelPackage(new FileInfo(templateFilePath), true))
{
var stream = new MemoryStream();
xls.SaveAs(stream);
stream.Position = 0;
return stream;
}
}
In controller:
return File(GenerateReport("filename.xltx"), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "filename.xlsx");
The file is created and saved succesfully but when I open it with Excel I get the error:
"We found a problem with some content in 'filename.xlsx'. Do you want
to try to recover as much as we can?".
I answer Yes and get:
"The workbook cannot be opened or repaired by Microsoft Excel because
it is corrupt".
I have verified that the template file is in the correct format and none of the other suggested solutions here on stackoverflow fixes the problem.
Any suggestions?
UPDATE:
I also tried without using a template and that works without problem:
using (var xls = new OfficeOpenXml.ExcelPackage())
{
xls.Workbook.Worksheets.Add("New");
var stream = new MemoryStream();
xls.SaveAs(stream);
stream.Position = 0;
return stream;
}
UPDATE 2:
Saving the .xltx template as a .xlsx and using the .xlsx as the template does not raise any errors when opened.