2011-10-25 13 views
0

phpとmysqlを使用して、データベースからexcelシートにデータをエクスポートしようとしています。 IEとすべてのブラウザでlocalhostで正常に動作しますが、IEの実際のWebサイト(hostmonsterサイト)で失敗します。ファイルをダウンロードできなかったことを示します。WebサイトのIEでphp export excelが動作しない

これは、動的データを生成して、ダウンロードを提供しています、私のサンプルコードです:

<?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; 
?> 

iは、ホストまたはIEで任意の設定を変更する必要がありますか?

+1

application/x-msexcelをieに送信せず、代わりにapplication/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');ヘッダー( 'Cache-Control:must-revalidate、post-check = 0、pre-check = 0'); header( 'Pragma:public'); –

1

あなたのファイルはapplication/vnd.ms-excelではありませんまたそれapplication/x-msexcelは、それがそのMIMEタイプがよくそうにもTSVのため正常に動作しますtext/csvを使用して知られていない可能性がしかしtext/tab-separated-valueshttp://www.iana.org/assignments/media-types/text/tab-separated-values

をだです。

また、Content-Disposition: attachmentを実行すると、ファイルを保存することを意味します。あなたがブラウザでそれを開くようにするには、inlineが必要です。