Question

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.

Was it helpful?

Solution

I answered a similar question here:

.xlsx Created and Saved Using Template with EPPlus is Unreadable/Corrupt

Short answer: EPPlus does not actually support xltx template files, the 'template' parameter just expects an appropriately pre-formatted xlsx file.

OTHER TIPS

I've recently found similar bug (Excel complained about some invalid xl/styles.xml section). After searching I came into solution: change version to 3.1.3 (3.1.2 has a bug). It seems 3.1.1 is ok too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top