17
header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xsl"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"

Here is code to write and download xsl file using php,
my problem is when i open excel file MS-Excel show warning before opening file says

The file you are trying to open is in different format than specified by the file extension...Blah blah

What's to do with PHP code to remove this warning? Contents are written correctly.

I know this is because content written in file are txt file content and file extension is incorrect, that is, xls. Solution?

Please don't suggest to use any library.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user794624
  • 367
  • 1
  • 5
  • 11
  • `Solution???` um - use the right extension, `.txt`? I'm not sure what you are asking here. – Pekka Mar 24 '12 at 23:17
  • 3
    But "Some Text" would not be valid content for an Excel file at all.... and Excel will give this message if you tell porky pies and try and pretend that a text file is an xls file... If you want to avoid this, don't tell us "No libraries". Use a library or write the whole thing yourself... the library I wrote has taken over 6 years so far – Mark Baker Mar 24 '12 at 23:26
  • 3
    Just sending the MIME type for Excel files won't magically turn HTML or CSV output into an actual Excel file. – mario Mar 24 '12 at 23:28
  • 2
    Of course, if you don't want to use a library, you could always simply enable the php_automagic extension for PHP by uncommenting it in your php.ini file. This tells PHP to automagically convert your plain text into a real Excel file to match the headers. – Mark Baker Mar 24 '12 at 23:29

5 Answers5

38

You are giving multiple Content-Type headers. application/vnd.ms-excel is enough.

And there are couple of syntax error too. To statement termination with ; on the echo statement and wrong filename extension.

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");  //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"; //no ending ; here
Starx
  • 77,474
  • 47
  • 185
  • 261
  • 2
    Not to mention the extension for the filename in Content Disposition – Mark Baker Mar 24 '12 at 23:23
  • @MarkBaker, Yeah, I missed that – Starx Mar 24 '12 at 23:27
  • I have copied and paste in a new php file and checked my downloded xls file it gives the same error message, Is something more to do to fix this ? – Alok Jha Mar 23 '16 at 08:21
  • I got the same error message , The file you are trying to open is in different format than specified by the file extension. – Alok Jha Mar 23 '16 at 09:20
  • I just added my code here : https://jsfiddle.net/alok111444/y97y31yt/ I don't know, how to open php editor in jsfiddle ,so I have simply paste code in html editor here – Alok Jha Mar 23 '16 at 09:24
  • @AlokJha, With above code, you will get a document with "Some Text" in the first cell. I did another test to make sure it is correct and it is. Which software are you using to test this? I was using LibreOffice. – Starx Mar 23 '16 at 13:36
  • @starx, Yes it is showing "Some Text" , but while opening xls file, there is popus comes that show an error message "The file you are trying to open is in different format than specified by the file extension." Is there some tool where I can upload the demo code ? – Alok Jha Mar 23 '16 at 19:31
  • @AlokJha, You can make a paste in pastebin and also show a screencast may be. – Starx Mar 24 '16 at 09:45
  • 1
    This is normal to have this popup come up. It is Excel's warning that you are not actually loading an excel file. When they save it, though, it will be right. If you want to make an actual Excel file to download, you need a server-side library which will allow you to do this. – Erick Robertson Oct 03 '16 at 21:54
  • Also, get rid of the expires and cache-control tags. They interfere with providing a fresh copy. – Erick Robertson Oct 03 '16 at 21:54
9

Try this

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
safarov
  • 7,793
  • 2
  • 36
  • 52
2

The problem is you typed the wrong file extension for excel file. you used .xsl instead of xls.

I know i came in late but it can help future readers of this post.

0

Just try to add exit; at the end of your PHP script.

clemens
  • 16,716
  • 11
  • 50
  • 65
0

For CI : In my case I was trying to export xlsx and the mime type was application/vnd.ms-excel , which was not added into the xlsx array so I added it manually in

application\config\mimes.php

and now it's working fine :

'xlsx'  =>  array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),

replaced the xlsx array with above , fixed for me !