1

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?

Ben James
  • 121,135
  • 26
  • 193
  • 155
JPro
  • 6,292
  • 13
  • 57
  • 83

5 Answers5

3

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

opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • For an example how to use PHPExcel see this: http://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=FAQ The list of PHPExcel features can be found here: http://phpexcel.codeplex.com/wikipage?title=Features&referringTitle=Documents – Adrian Oct 27 '09 at 11:20
2

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
  • 6,899
  • 9
  • 43
  • 52
  • 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 Oct 27 '09 at 10:44
  • Is the database query 'expensive'? I.e. does it take a lot of time to run? – KB22 Oct 27 '09 at 10:48
  • There are lot of records in the results page. So is there any alternate way to get these results without requery? – JPro Oct 27 '09 at 10:51
  • Then you need to save them temporarily, but I agree to Adrian: you'll find solutions for this in the mentioned posts. – KB22 Oct 27 '09 at 10:53
2

This has been discussed before at StackOverflow:

Export MYSQL result to Excel..

PHP code to convert a MySQL query to CSV

See also these examples and tutorials:

Easy way to create XLS file from PHP

Export MYSQL data to CSV – PHP tutorial

PHP Script: Export MYSQL Table Data to CSV

Advanced CSV Export

Community
  • 1
  • 1
Adrian
  • 6,013
  • 10
  • 47
  • 68
1

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.

Martin Nycander
  • 1,309
  • 13
  • 29
  • +1: Agree, but http://www.codeplex.com/PHPExcel makes creating Excel files easy too. – KB22 Oct 27 '09 at 10:42
  • 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 Oct 27 '09 at 10:44
  • 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 :) – Martin Nycander Oct 27 '09 at 10:57
1

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
  • 6,659
  • 5
  • 35
  • 68
  • 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 Oct 27 '09 at 10:50