1

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!

4
  • What do yo exactly get from server? Commented Jun 3, 2014 at 11:30
  • your script working for me Commented Jun 3, 2014 at 11:34
  • @ZeroWorks hi, i get some html code and js code. Now i changed to what mHouses suggested, i see the testing data and whole source code. Commented Jun 3, 2014 at 12:10
  • if you're getting html from this, then you've got some OTHER code somewhere that's spitting out that html. Commented Jun 3, 2014 at 14:18

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks for reply. Yes, the thing is that i do have other content, i guess that is why i get excel file with the html code. :(
Just put ob_start(); before sending anything, and then ob_get_flush(); before the header and it will work.
like ob_start(); 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"; ob_get_flush();?
unfortunately, it doesnt help. :(
Hi, what i get now in excel is the two testing lines, and also the entire site's source code, if i dont put a die() after echo.
|
1

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.

2 Comments

Hi, thanks for reply. The app has other content, in header and container, so i guess thats why i get an excel with html code, instead of the testing data.
Probably that's the cause, or maybe that it isn't formatting your html info as a table. However, your plain code does works here. Good luck.
0

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

4 Comments

Hi, thanks for help! A little bit better, at least i see the testing data, but still I see the whole source code in the excel file. :(
try putting a "die" after the last echo. It could be that your application is sending more content after the csv.
yes, it works. But it is not optimal to put a die() there, right
@user3382722 Maybe it isn't optimal to put a die() there, but you MUST do something to stop the script or, at least, to stop sending more data to the client.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.