I have a simple php file for example:

<?
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=file123.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "test";
?>

What I want to do is export php result and save it in excel file. If I run this from web browser like IE,firefox, I have no problem to generate excel file on my local machine.
However I want to run this from command line at background, instead of web browser, now if I open command prompt box and run it, it just print result "test" on command box, there is no export file generated.

Why??? how can I fix this issue?

Any advice will be appreciated.

It looks like your last line is wrong. Also, try this version of the first line. This is what I use:

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print $data;


$data needs to hold the tab delimited text file that you create. Your line just tells the routine to print the string "test". You are not passing any data.

You have to format the data. Separate your fields with chr(9) and end each line with chr(9).

Thanks for reply.
I tried this, it works ok from web browser, but not from command line, it doesn't generate excel file.
Actually this $data is the dynamic php web page I created, I want to save this php page into excel file, from command line.

Spreadsheets are for tabular data. How do you plan to break a web page up into cells? Are you talking about data that you are displaying on a PHP page?

I'm not sure how you would do this from a command line.

The php page contains table that display dynamic data retrieved from mysql database, I have no problem to display this table on web browser and convert it to excel file, by manually.
Now I need to set cronjob to run this php script at backend to retrieve data from mysql DB and generate excel file in weekly basis, I got stuck here.
Any idea?

This article has been dead for over six months. Start a new discussion instead.