PHP Excel Header

2019-01-14 23:17发布

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"

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

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

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.

3条回答
Rolldiameter
2楼-- · 2019-01-14 23:26

Try this

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
查看更多
我只想做你的唯一
3楼-- · 2019-01-14 23:27

Just try to add exit; at the end of your PHP script.

查看更多
等我变得足够好
4楼-- · 2019-01-14 23:29

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

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
查看更多
登录 后发表回答