Liam de Haas
Liam de Haas

Reputation: 1268

File format and extension do not match

I'm using the php-export-data library to export some data to an excel document.

I've initialized, added rows and finalized the document as per example and it generates a usable .xls file as expected but when I open it Excel 2010 throws a warning message saying the file format and extension of xls don't match.

The file is still usable and can be opened fine but as this is for a client I would much prefer for the warning to not show up at all.

The code I use to initialize, add rows and finalize the document.

$mailing_list = $event->getMailingList(); //returns an array

$exporter = new ExportDataExcel('browser', 'mailing_list.xls');
$exporter->initialize();

foreach($mailing_list as $mail){
    $exporter->addRow($mail);
}

$exporter->finalize();

When looking at the Microsoft documentation about this error it comes down to .xls files being renamed to .xlsx files and vice versa. I've tried using mailing_list.xlsx instead of mailing_list.xls but then Excel can't open the file.

I haven't changed anything to the library so maybe the problem lays there?

Upvotes: 1

Views: 9681

Answers (2)

Axel Richter
Axel Richter

Reputation: 61975

The warning appears because the file extension *.xls is different from the content which is XML.

As in https://github.com/elidickinson/php-export-data mentioned, this library will "export data in CSV, TSV, or Excel XML (aka SpreadsheeML) format to a file or directly to the browser".

So the real content type of its exports will be XML and not XLS or XLSX. If you name the file *.xml and open this *.xml with Excel then the warning will disapear. But then you can't be sure that this file is opened in Excel by double click. It will not if there is another application set as the default application for *.xml files.

So either you live with that warning or name the file *.xml or you use a library which can really export XLS and/or XLSX. https://github.com/PHPOffice/PHPExcel for example.

Upvotes: 4

Phylogenesis
Phylogenesis

Reputation: 7890

*.xlsx files can only be opened with Excel 2007 or later by default. If your version of Excel is older than this, then you can download and install the Microsoft Office compatibility pack.

Looking at the comments above the ExportDataExcel class, the format of the file is called SpreadsheetML. Unfortunately, it seems the correct extension for files of this type is probably *.xml which will not open in Excel by default. The comments mention that you can get around this issue by giving the file a *.xls extension, but that this will bring up the warning you get.

Upvotes: 1

Related Questions