App using PHPExcel ... the Excel writer/ export (.xls extension) gives Excel 2013 error (file extensions don't match). How to solve?

Feb 19, 2015 at 6:56 PM
Hello there--

I'm using an application online that uses PHPExcel to manage a kind of virtual Excel document. It that allows exports in PDF, CSV, or Excel formats.

However, the Excel format causes errors (even though it looks identical to the CSV format).

Excel says "The file format and extension of "yaddayadda.xls" don't match. The file could be corrupted or unsafe. Unless you trust it's source, don't open it."

I think this has to deal with extension hardening:

Anyway as a big rookie with PHPExcel, what part of the source code does the function that writes .XLS/ Excel documents found, so I can change that to just write CSVs? Help appreciated, thanks.
Feb 20, 2015 at 1:20 AM
I am having the same issue, upgraded to the latest version to try and solve this, any help would be much appreciated!
Feb 20, 2015 at 2:51 PM
A BIFF-format Excel file (.xls) bears no resemblance to a CSV file: if you are writing a csv file with a .xls extension, then you may well get the warning explained in the MSDBN blog entry because .xls extension assumes BIFF-format and a csv file isn't a BIFF-format file..... the solution is to use the appropriate extensions for the appropriate file types.

When you write a file with PHPExcel, you select a Writer, e.g
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
to select the Excel5 Writer for BIFF-format (.xls) files, or
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
to select the Excel2007 Writer for OfficeOpenXML (.xlsx) files, or
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
to select the CSV Writer for CSV-format files

Then you call the save() method for that Writer instance

This is all documented