PHP Excel 标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9856518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Excel Header
提问by user794624
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
这是使用 php 编写和下载 xsl 文件的代码,
我的问题是当我打开 excel 文件时 MS-Excel 在打开文件之前显示警告说
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.
删除此警告与 PHP 代码有什么关系?内容写得正确。
I know this is because content written in file are txt file content and file extension is incorrect, that is, xls. Solution?
我知道这是因为file中写的内容是txt文件内容,文件扩展名不对,也就是xls。解决方案?
Please don't suggest to use any library.
请不要建议使用任何库。
回答by Starx
You are giving multiple Content-Type
headers. application/vnd.ms-excel
is enough.
您正在提供多个Content-Type
标题。application/vnd.ms-excel
足够。
And there are couple of syntax error too. To statement termination with ;
on the echo statement and wrong filename extension.
而且也有几个语法错误。To 语句终止;
于 echo 语句和错误的文件扩展名。
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
回答by safarov
Try this
尝试这个
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
回答by Masaba James Moses
The problem is you typed the wrong file extension for excel file. you used .xsl instead of xls.
问题是您为 excel 文件输入了错误的文件扩展名。您使用了 .xsl 而不是 xls。
I know i came in late but it can help future readers of this post.
我知道我来晚了,但它可以帮助这篇文章的未来读者。
回答by Petko Doichev
Just try to add exit;
at the end of your PHP script.
只需尝试exit;
在 PHP 脚本的末尾添加。