Forum Moderators: coopster
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=mydata.csv");
header("Pragma: no-cache");
header("Expires: 0");
Now just have the rest of the php print out your variables in rows separated by commas and you're ready to go.
The thing I do is inject the date.
header("Content-disposition: attachment; filename=mydata" . date("Y-m-d") . ".csv");
You could stick in your own variable..
header("Content-disposition: attachment; filename=" . $myfilename . ".csv");
Otherwise just let the user decide. At least on Windows the user is left with the option to save it to the place and filename that they choose.
If there are commas, or line-breaks in any of the fields, then of course the csv file becomes a mess. I have replaced the commas with dashes, and that works for now, and I'm trying to replace the line-breaks with periods, and I assume that I'll get that working, but is there some way to keep commas and/or line-breaks in the data?
Here's what I'm doing for each field where I might have a comma or a line-break:
$trans = array("\r\n" => ". ", ","=>"-");
$value=strtr($value,$trans) hm, I'll bet there's no way to keep a line break in a csv - but how 'bout commas?
$trans = array("\r\n" => ". ");
$value='"'.strtr($value,$trans).'"'; And although my fields with commas didn't get broken, the commas disappear entirely, making for a real mess!
Is adding quotes a good idea for some other reason I should be worried about?
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=tracking_".$variable."_".date("d-m-y").".csv");
header("Pragma: no-cache");
header("Expires: 0");
// print out the cvs file column heads
echo "column,head,names\n";
// get that data!
$sql="SELECT var1,var2,var3 FROM table"
$result=mysql_query($sql);
while ($info = mysql_fetch_array($result)){
// print out the data, line by line.
echo $info['var1'].",".$info['var2'].",".$info['var3']."\n";
} It's working fine, but I'm testing with only a few records. Is that the right order to do things, or should I build up my text first, then send the header and the text?
I think it will work either way.. you can either generate the list first, then echo everything; or you can echo it on-the-fly. The only concern I can think of with echoing it on-the-fly is if the user is waiting for output because the server is still compiling the list. But since this is over the web, it's most likely that your server will be able to keep up with the internet connection, so this shouldn't be a concern.
Sorry to be thick-headed here, but I'm not sure why? It's working perfectly, at least for me, without the quotes. The file is opening up in excel very nicely.
You're not thick-headed... I'm just anal ;-) It's just standard CSV format. While Excel might be forgiving others applications may not. Kinda like html standards, eh?
Timotheos, many thanks for explaining. I see your point, and yes, sounds like html standards :)
In this specific case, the file will *always* be opened in excel, but it's good to know this sort of thing for... next time!
Many thanks again to all! :))