【问题标题】:php export to excel does not work in IE in websitephp导出到excel在网站的IE中不起作用
【发布时间】:2011-10-25 07:58:40
【问题描述】:

我正在尝试使用 php 和 mysql 将数据从数据库导出到 Excel 工作表。 它在 IE 和所有浏览器中的本地主机中运行良好,但在 IE 中的真实网站(hostmonster 站点)中失败。它说文件无法下载。

这是我生成动态数据并提供下载的示例代码:

<?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 的任何设置吗?

【问题讨论】:

  • 你有没有试过不发送application/x-msexcel给ie,而只发送application/vnd.ms-excel?只需对 $_SERVER["HTTP_USER_AGENT"] 做一个简单的 if 检查即可。

标签: php internet-explorer export-to-excel


【解决方案1】:
$filename ='excelreport.xls';
$contents = "testdata1 \t testdata2 \t testdata3 \t \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;

【讨论】:

  • 这几乎可以正常工作,但我必须添加一些额外的标题才能使其在 IE 中工作。 header('内容传输编码:二进制'); header('过期时间:0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public');
【解决方案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。 (有些浏览器允许用户覆盖它,有些则不允许。)

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2015-05-22
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多