PHP Excel 标头

2022-01-11 00:00:00 header php phpexcel fwrite
header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xsl"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"

这是使用 php 编写和下载 xsl 文件的代码,
我的问题是当我打开 excel 文件时 MS-Excel 在打开文件之前显示警告说

Here is code to write and download xsl file using php,
my problem is when i open excel file MS-Excel show warning before opening file says

您尝试打开的文件的格式与文件扩展名指定的格式不同...废话

The file you are trying to open is in different format than specified by the file extension...Blah blah

如何处理 PHP 代码来消除此警告?内容书写正确.

What's to do with PHP code to remove this warning? Contents are written correctly.

我知道这是因为文件中写入的内容是txt文件内容,文件扩展名不正确,即xls.解决方案?

I know this is because content written in file are txt file content and file extension is incorrect, that is, xls. Solution?

请不要建议使用任何库.

Please don't suggest to use any library.

推荐答案

您提供了多个 Content-Type 标头.application/vnd.ms-excel 就够了.

You are giving multiple Content-Type headers. application/vnd.ms-excel is enough.

还有一些语法错误.在 echo 语句和错误的文件扩展名上使用 ; 终止语句.

And there are couple of syntax error too. To statement termination with ; on the echo statement and wrong filename extension.

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");  //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"; //no ending ; here

相关文章