1

I am working on a feature in which I am proving the excel dump to the user. When user downloads the excel sheet it works fine. the moment user tries open with Microsoft Excel it gives me an error "Excel cant open the file because the file format or the extension is not valid" This is happening only in firefox browser, and in file name .xlsx extension is added automatically.

Code I am using to create the excel dump is

    header('Pragma: public');
    header("Expires: Mon, 12 Jul 2012 05:00:00 GMT");                // Date in the past
    header('Content-Disposition: attachment; filename="excel.xls"');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0'); 

   $header  = "Date\t";
   $header .= "Other Details Details\t";
   echo $header . "\n";
   echo "Date1\t";
   echo "Detail1\t\n";
   echo "Date2\t";
   echo "Detail2\t\n";
Ashish
  • 271
  • 2
  • 15
  • .xlsx files can be opened only in Microsoft Excel 2007+, are you sure that those computers where you're trying to open it has that version of Excel? – Hoh Dec 27 '13 at 10:00
  • Yes I am using Excel 2007 and moreover the format which I am trying to download in is .xls – Ashish Dec 27 '13 at 10:04
  • did you find the root cause & solution for this ? – yathirigan Dec 10 '15 at 19:14

1 Answers1

0

I had this exact issue with the following (mismatching) response headers:

Content-Disposition: attachment; filename="excel.xls"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

The content type indicates an .xslx file, which apparently causes Firefox to add the .xslx extension. Excel shows the error because the file extension (appended by Firefox) is .xslx, while the file format is xls.

To solve this issue, change the content type to:

Content-Type: application/vnd.ms-excel
Philip Bijker
  • 4,955
  • 2
  • 36
  • 44