1

I'm generating an EEPlus .xlsx file that the user can download after clicking a button. It won't open on my Win8 machine with LibreOffice 4.4.1.2, but it will open on my coworkers Win8 machine using Excel 2013.

I get an error saying the file is corrupt and needs to be repaired, and when I try to repair it, it says it's unable to. My coworker also gets this error but is able to repair it and view the file.

Here's the relevant code for the creation of the downloadable version:

MemoryStream ms = DataTableToExcelXlsx(dt, "Report");
 ms.Position = 0;
 ms.WriteTo(Response.OutputStream);
 Response.ContentType="application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet";
 Response.AddHeader("Content-Disposition", "attachment;filename=DataTable.xlsx");
 Response.StatusCode = 200;

Now the weird part is that I'm also generating an xlsx file that is attached to an email, and it opens just fine. Neither me nor my coworker receive the error when opening the file attached to to the email.

Relevant code for the creation of the email attached version:

MemoryStream ms = DataTableToExcelXlsx(dtReportData, sReportTitle);
ms.Position = 0;
MailMessage mail = new MailMessage("you@yourcompany.com", sReportEmails);
using (SmtpClient client = new SmtpClient())
{
    mail.Subject = "Mixer - Test Scheduler Task Execute.";
    mail.Body = string.Format("The following report is executing. ReportSettings Id: {0}", reportSettingsId);
    mail.Attachments.Add(new Attachment(ms, sReportTitle + ".xlsx", "application/vnd.ms-excel"));
    client.Send(mail);
}

I've also tried using the vnd.ms-excel MIME type for the downloadable version and that didn't work, so I'm at a loss as to why this is happening.

enthe0s
  • 71
  • 5

2 Answers2

0

I would have liked to answer this as a comment, but no reputation yet, or whatsoever.

Firstly, Why dont you try to use this content type? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Currently, it does seem to be wrong. Please see as well this post, in which correct mime types are defined.

Community
  • 1
  • 1
Javi bl
  • 83
  • 2
  • 7
  • So my problem is with the downloadable version, where I'm using that content type already. The email version is using the `vnd.ms-excel` type, which currently works. – enthe0s Mar 26 '15 at 20:53
  • @enthe0s OK, let me know if I am mistaken; on your downloadable version you have used both "application/vnd.ms-excel" (which should be used only for xls, not for EPPlus' openXML docs) and "application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet", which is incorrect. Is that what you mean? – Javi bl Mar 26 '15 at 21:04
0

Try making a unit test or command line app that creates the xlsx to your local hard drive (avoiding all the the web Response stuff). Then see if the error shows up in that xlsx.

If it does then it is the code that create the file. Post it up and we can take a look.

If it does not then something is up with the response settings. Maybe in the disposition? Here I have an Response stream that I know works: Open ExcelPackage Object with Excel application without saving it on local file path

Community
  • 1
  • 1
Ernie S
  • 13,902
  • 4
  • 52
  • 79