user3382722
user3382722

Reputation: 35

php generating excel and downloading

i have a website with one simple function to generate an excel with array data and then offers user to download.

header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";

the code above was used to test, but I only get some html code from the website in the excel, not the data.

May I ask why it happens? Thanks!

Upvotes: 1

Views: 14402

Answers (3)

ZeroWorks
ZeroWorks

Reputation: 1638

Make sure you don't send anything before header calls.

// At the begginnig of script...
ob_start();

// ... do some stuff ...

ob_get_clean();

header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
die();

Another approach if you need to process entire script:

<?php
// At the beggining...
ob_start();    
$content="";
$normalout=true;

// ... do some stuff ...

// i guess if some condition is true...
$content=ob_get_clean();
$normalout=false;
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";

// Here you could die() or continue...
ob_start();

// ... rest of execution ...

$content.=ob_get_clean();
if($normalout)
{
    echo($content);
} else {
      // Excel provided no output.
}
?>

This should work.

Upvotes: 4

mHouses
mHouses

Reputation: 925

You are sending a file that is a tab-sepparated values, not an XLS (which is a binary format).

What you can do is send the file as a CSV (Content-type="text/csv"), and the file formatted as csv:

header("Content-Type: text/csv");
header("content-disposition: attachment;filename=my_document.csv");
echo '"First Name";"Last Name";"Phone"' . "\n";
echo '"John";"Doe";"555-123456"' . "\n";

Upvotes: 0

AleksanderKseniya
AleksanderKseniya

Reputation: 347

That code seems to work on Chrome without any issue (using php tags, of course).

Here you have a template, anyway, that I use for exporting POST tables that are being sent.

<?php
        header("Content-Type: application/vnd.ms-excel");
        header("Expires: 0");
        $date = date('Y-m-d-Hi');
        header("content-disposition: attachment;filename=Excel_Report_$date.xls");
        $table = $_POST["table_info"];
?>

<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
                <table>
                        <tr><th>&nbsp;</th></tr>
                        <tr><td>&nbsp;</td></tr>
                        <?=$table?>
                </table>
        </body>
</html>

If you are exporting some reports that would require some more detail, I would recommend PHPExcel as by far the best library out there for CSV/XML/XLS(X) management via PHP code.

Upvotes: 1

Related Questions