2011-10-25 43 views
0

我想從數據庫導出數據到Excel表,使用PHP和MySQL。 它在IE和所有瀏覽器中的本地主機上正常工作,但在IE(主機監控站點)的真實網站中失敗。它說該文件無法下載。PHP出口到Excel無法在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瀏覽器的任何設置?

+1

您是否嘗試過不發送應用程序/ x-msexcel的IE瀏覽器,而是隻發送應用程序/ vnd.ms - Excel中?只要做一個簡單的檢查$ _SERVER [「HTTP_USER_AGENT」]。 –

回答

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; 
+0

這幾乎工作,但我不得不添加一些額外的頭讓它在IE中工作。 header('Content-Transfer-Encoding:binary'); header('Expires:0'); header('Cache-Control:must-revalidate,post-check = 0,pre-check = 0'); header('Pragma:public'); –

1

您的文件不是application/vnd.ms-excel也不是application/x-msexcel它的text/tab-separated-valueshttp://www.iana.org/assignments/media-types/text/tab-separated-values

然而MIME類型可能不是衆所周知所以使用text/csv這將工作的TSV確定爲好。

此外,當你做Content-Disposition: attachment它意味着保存該文件。如果你想讓瀏覽器在Excel中打開它,你需要inline。 (有些瀏覽器讓用戶覆蓋此,有些則沒有。)

相關問題