php 导出到 excel 在网站的 IE 中不起作用

发布于 2024-12-11 21:38:17 字数 1067 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-12-18 21:38:17
$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;
$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;
水染的天色ゝ 2024-12-18 21:38:17

您的文件不是 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 it application/x-msexcel it's text/tab-separated-values http://www.iana.org/assignments/media-types/text/tab-separated-values

However 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 need inline. (Some browsers let the user override this, some don't.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文