Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPExcel为文件格式设置特定的头_Php_Header_Phpexcel - Fatal编程技术网

PHPExcel为文件格式设置特定的头

PHPExcel为文件格式设置特定的头,php,header,phpexcel,Php,Header,Phpexcel,在谷歌搜索时,我发现在输出以不同文件格式生成的excel时需要设置两组不同的标题 例如 对于类型“Excel5”的标题为: 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-Typ

在谷歌搜索时,我发现在输出以不同文件格式生成的excel时需要设置两组不同的标题

例如

对于类型“Excel5”的标题为:

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 ");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
对于类型“Excel2007”的标题为:

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 ");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
我的问题:是否需要为每种文件类型设置不同的标题,因为还有其他文件类型CSVHTMLPDF

header("Pragma: public");
不,这是错误的,尽管很多人认为这与缓存有关

header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
与Excel无关-这些只是控制缓存

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
否-应该只有一个内容类型标题。对于使用OLE的MS Excel文件,mimetype应为application/vnd.MS-Excel

只有上面的第二个头是有效的mime类型

header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
第二个标头是冗余的,前者指定下载的文件名

内容类型:application/vnd.openxmlformats of cedocument.spreadsheetml.sheet')

仅适用于.xlsx文件(即以XML格式保存)。否则,应使用application/vnd.ms-excel。事实上,后者应该是向后兼容的

我的问题:是否需要为每种文件类型设置不同的头

是-内容类型标题是文件类型。但只有这个标题需要更改

C