/    Sign up×
Community /Pin to ProfileBookmark

PHP to download into Excel

Hi,

I have a webpage which displays data in report views format. So basically a table. I am looking for a php script of somesort that will download the data into excel.

I have the below which opens up excel but the data does not download.

<?php
$filename = “excel download.xls”;

header(“Pragma: public”); $display=”attachment”;
header(‘Content-Type: application/vnd.ms-excel’);
header(“Content-Disposition: $display; filename=$filename”);
header(“Cache-Control: no-cache”);

?>
Thanks,

Baz

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Jan 27.2011 — After you send those headers you need to echo out the data for it to send, obviously the data has to be in the correct format. You may have better luck with exporting out a .csv rather than an excel file.
Copy linkTweet thisAlerts:
@TeaserauthorJan 28.2011 — Hi Jarrod,

Would you be able to give me the code to echo out the data.. if possible for the csv as well?
Copy linkTweet thisAlerts:
@Jarrod1937Jan 28.2011 — I believe the .xls is a binary file format, so its not just something you can just print out (though there are libraries for this i believe). However, a .csv is relatively easy:

[code=php]
// Newline character to use
$newline="rn";

// array of items to filter, will interfere with the format
$filterarray=array("rn", "n", "r");
$maincsvstring='';

// Have a while statement to loop through your data
while(){

/* Append to string. The str_replace is fed the filterarray from above.
The method below allows you to filter the entire line of the string at once,
a bit more effective than filtering each string output individually.
Notice the $newline appended to the string, important to delimit a new record
*/
$maincsvstring.=str_replace($filterarray,'',$output1.','.$output2.','.$output3).$newline;
}

// Stripped to the minimum amount of headers needed for brevity
header("Content-type: application/ text");
header("Content-disposition: attachment; filename=your_csv_file.csv");

// Echo string out. With the above headers this will force a download
// dialog and will stream the output to the file.
echo $maincsvstring;
[/code]


Although i didn't do it above, you may want to consider removing ,'s if your data contains them in a non-encapsulated way (not within quotes " ").
Copy linkTweet thisAlerts:
@TeaserauthorJan 28.2011 — cheers mate.. i'll give this a go and see what happens ? will let you know monday/tuesday.
×

Success!

Help @Teaser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...