tags:

views:

357

answers:

5

HI,

I am trying to display results from a database in results.php. In the same file, after all the data has been displayed in the current webpage,I am placing a button to create a file based on this results to produce a Excel file.

I am not sure how to do this. I have tried but it says you have to force excel file download before any echo, but I want to display as well as produce an excel file. Is this possible or am I missing something? can anyone please let me know how to do this correctly?

+1  A: 

You could create a .cvs-file which are a lot simpler than xls-files, and the best part is that excel supports .cvs-files.

Then while you are outputting your data to the user, have a seperate variable called something like $cvs which you apply the same data as you output, just in the .cvs-format and when you're done you write $cvs to a file.

Then you just link to that file at the bottom of the page.

You may have to force the download upon the user though.

Morningcoffee
+1: Agree, but http://www.codeplex.com/PHPExcel makes creating Excel files easy too.
KB22
Yes, I want to use CSV file indeed. But the same thing applies that I want to prepare this data and allow the user to download it on demand.
JPro
Yeah, I meantion in my answer how you should go along to do that. Maybe I didn't make myself clear -- so I edited the post :)
Morningcoffee
+1  A: 

You're probably faceing an 'Header allready set' error. You need to separe the two steps in order to make that work. One step for displaying and one for excel file creation and downloading.

On th other hand you could combine the data display and excel creation too and eventually display a download link.

Just pokin in the dark, let us know which way you wanna go and we'll be helping you along...

KB22
Yes I can combine the display and creation, but I am leaving the option to user if he wants to download the results in excel file or not. Can you tell me the two step process?
JPro
Is the database query 'expensive'? I.e. does it take a lot of time to run?
KB22
There are lot of records in the results page. So is there any alternate way to get these results without requery?
JPro
Then you need to save them temporarily, but I agree to Adrian: you'll find solutions for this in the mentioned posts.
KB22
+3  A: 

Hi, you are right:

You have to change the header for the page, so you have to open the output for the excel file in a new window.

Header type: "application/vnd.ms-excel" .

If you want to create "good" Excel sheets , take a look on PHPExcel

PHPExcel

ArneRie
Adrian
+1  A: 

Set your headers first and then echo your CSV output:

header("Content-type: application/text/x-csv");
header("Content-disposition: attachment; filename=report.csv");
echo "value1,value2\n";
echo "value3,value4\n";
Trevor
Yes, I can do thos, But my main concern is, I have already displayed the results from the database, in the first page, if I create a button to open in new window, with the above code, then how can I get the data that is already displayed? do you want me to run the query again? is there any precedure for this?
JPro