我试图从数据库导出数据到excel表,使用php和mysql。它在我的本地主机在IE和所有浏览器中工作良好,但在IE的真实网站(一个hostmonster网站)失败。提示文件无法下载
这是我的示例代码,生成动态数据,并提供下载:
<?php
while($node_stmt = mysql_fetch_array($result))
{
$contents.="tt".$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的任何设置吗?
$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;
你的文件不是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
。(有些浏览器允许用户覆盖这个,有些则不)