Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel set specific headers for file format

While googling I found two different sets of headers that need to be set when outputting excel generated in different file format.

for e.g.

For Type "Excel5" headers are:

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=$filename");
header("Content-Transfer-Encoding: binary ");

For Type "Excel2007" headers are:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

My question: is there need to set up different headers for each file type as there are other file types also CSV, HTML and PDF?

like image 587
Asif Avatar asked Apr 08 '10 06:04

Asif


1 Answers

header("Pragma: public");

No - this is just wrong - though lots of people think it has something to do with caching

header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

Nothing to do with Excel - these just control caching

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;

No - there should only be one content-type header. For a MS Excel file using OLE, the mimetype should be application/vnd.ms-excel

Only the second header above is a valid mime type.

header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");

The second header is redundant, the former specifies a filename for the download.

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

Only for a .xlsx file (i.e. saved in XML). Otherwise you should use application/vnd.ms-excel. Indeed the latter should be backwardly compatible.

My question: is there need to set up different headers for each file type

Yes - the Content-Type header is the file type. But only this header needs to change.

C.

like image 119
symcbean Avatar answered Oct 16 '22 19:10

symcbean