Reputation: 2289
I'm generating and using the following sample XML to generate and download an XLSX file.
<!--?xml version="1.0"?-->
<!--?mso-application progid="Excel.Sheet"?-->
<ss:workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:worksheet ss:name="Title">
<ss:table>
<ss:column>
<ss:row>
<ss:cell><ss:data ss:type="String">Something</ss:data></ss:cell>
<ss:cell><ss:data ss:type="String">Something</ss:data></ss:cell>
</ss:row>
</ss:column>
</ss:table>
</ss:worksheet>
</ss:workbook>
I then use the following code snippet to force the file to download.
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
;
header("Content-Disposition: attachment;filename=something.xml ");
header("Content-Transfer-Encoding: binary ");
print $this->file;
?>
The file downloads correctly; however, the extension is XML. I can manually open it in Excel and it would show correctly. I've tried changing the filename
in the Content-Disposition
line to something.xlsx
, but when I try to open the file by double-clicking on it, I get a warning that:
Excel cannot open the file something.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Any idea what I'm doing wrong? Is the XML not in the right format?
Upvotes: 1
Views: 1974
Reputation: 42715
You only need to set the Content-Type once, and you should set it to the correct value:
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=something.xlsx");
header("Content-Transfer-Encoding: binary");
echo $this->file;
Also best to leave off trailing ?>
so that no stray whitespace gets into the downloaded file.
Upvotes: 1