Search code examples
phpinternet-explorerexport-to-excel

php export to excel does not work in IE in website


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?


Solution

  • $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;