php 导出到 excel 在网站的 IE 中不起作用
我正在尝试使用 php 和 mysql 将数据从数据库导出到 Excel 工作表。 它在 IE 和所有浏览器中的本地主机中工作正常,但在 IE 中的真实网站(主机怪物站点)中失败。它说该文件无法下载。
这是我的示例代码,它生成动态数据并提供下载:
<?php
while($node_stmt = mysql_fetch_array($result))
{
$contents.="\t\t".$node_stmt['uniqueid']."\t";
$contents.=$node_stmt['title'].' '.
ucfirst($node_stmt['firstname']).' '.
ucfirst($node_stmt['lastname'])."\t";
$contents.=$node_stmt1['topic']."\t";
$contents.=$abst."\t";
$contents.=$node_stmt['email']."\t";
$contents.=$node_stmt['phoneno']."\t";
$contents.=$pay."\t";
$contents.=$node_stmt['state_id']."\t";
$contents.=$node_stmt['country_id']."\n";
}
.....
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
header("Content-type: application/x-msexcel"); // This should work for the rest
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
echo $contents;
?>
我需要更改主机或 IE 的任何设置吗?
I am trying to export data from database to an excel sheet, using php and mysql.
It works fine in my localhost in IE and all browsers, but fails in the real website in IE (a hostmonster site). It says the file could not be downloaded.
This is my sample code that generates dynamic data and offers download:
<?php
while($node_stmt = mysql_fetch_array($result))
{
$contents.="\t\t".$node_stmt['uniqueid']."\t";
$contents.=$node_stmt['title'].' '.
ucfirst($node_stmt['firstname']).' '.
ucfirst($node_stmt['lastname'])."\t";
$contents.=$node_stmt1['topic']."\t";
$contents.=$abst."\t";
$contents.=$node_stmt['email']."\t";
$contents.=$node_stmt['phoneno']."\t";
$contents.=$pay."\t";
$contents.=$node_stmt['state_id']."\t";
$contents.=$node_stmt['country_id']."\n";
}
.....
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
header("Content-type: application/x-msexcel"); // This should work for the rest
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
echo $contents;
?>
Do i need to change any settings with the host or IE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的文件不是
application/vnd.ms-excel
也不是application/x-msexcel
它是text/tab-separated-values
http://www.iana.org/assignments/media-types/text/tab-separated-values但是该 mime 类型可能并不为人所知,因此请使用
text/csv
对于 tsv 来说也可以正常工作。此外,当您执行
Content-Disposition: Attachment
时,这意味着保存文件。如果您希望浏览器在 Excel 中打开它,您需要inline
。 (有些浏览器允许用户覆盖它,有些则不允许。)You file is not
application/vnd.ms-excel
nor is itapplication/x-msexcel
it'stext/tab-separated-values
http://www.iana.org/assignments/media-types/text/tab-separated-valuesHowever that mime type may not be well known so use
text/csv
which will work OK for tsv as well.Also when you do
Content-Disposition: attachment
it means to save the file. If you want browsers to open it in excel you needinline
. (Some browsers let the user override this, some don't.)