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

Asi*_*sif 8 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-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
Run Code Online (Sandbox Code Playgroud)

对于类型"Excel2007"标题是:

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

我的问题:是否需要为每种文件类型设置不同的标题,因为还有其他文件类型还包括CSV,HTMLPDF

sym*_*ean 12

header("Pragma: public");
Run Code Online (Sandbox Code Playgroud)

不 - 这是错的 - 虽然很多人认为它与缓存有关

header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
Run Code Online (Sandbox Code Playgroud)

与Excel无关 - 这些只是控制缓存

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
Run Code Online (Sandbox Code Playgroud)

不 - 应该只有一个内容类型标头.对于使用OLE的MS Excel文件,mimetype应为application/vnd.ms-excel

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

header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");
Run Code Online (Sandbox Code Playgroud)

第二个标头是冗余的,前者指定下载的文件名.

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

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

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

是 - Content-Type标头文件类型.但只有这个标题需要改变.

C.