You're not really generating an Excel file. You're generating what amounts to a .csv file using tabs as the seperator.
To generate a 'real' Excel file, use PHPExcel.
The problem is that when Excel sees a file ending in .xls, it expects the file to conform to its BIFF standard. Your code does not produce a BIFF-compliant file. If you want, I can provide you with PHP functions that produce BIFF-compliant files, but it's too long to post here.
You seem to be generating a file with tab-separated fields, and are claiming in your headers that this is the same format as a binary Excel spreadsheet uses. It isn't the format Excel uses. And the program loading the spreadsheet is letting you know that you are misleading people - possibly just yourself.
You should probably look into using an export format such as a CSV file; converting from tab-delimited to comma-separated is not going to be hard.
If you're just going to create a tab delimited file, why not skip PHP and let MySQL do it for you with SELECT INTO OUTFILE
?
PHP can handle the download part. Set the Content-type header to text/plain. Excel knows how to open that.
Your program is looking for a standard .xls
document, which is entirely different than what you are outputting. Your program outputs a tab delimited sheet, which is a .txt
file. Try switching your program from tabs to commas as delimiters and saving as a .csv
(comma separated value).
Try PEAR's (PHP Extension and Application Repository) Spreadsheet_Excel_Writer. I use that to generate Excel files in the BIFF format.