Hi all,
Does anyone know how to write data into csv file? I was using fwrite() to do this now but all its data will write into the 1st cell of the csv file only. Thanks in advance for anyone who offers help.
how to write data into csv(excel) file?
Moderator: General Moderators
-
jeffery
- Forum Contributor
- Posts: 105
- Joined: Mon Apr 03, 2006 3:13 am
- Location: Melbourne, Australia
- Contact:
The manual has an example:
You need to use the "Append" mode
http://php.net/fwrite
cheers,
Jeffery
Code: Select all
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}http://php.net/fwrite
cheers,
Jeffery
If its only in the first cell then you also need to add in commas or tabs (however its delimited) then add new lines to the end of each line, if you open it up in a a text editor, you want the contents of your csv file to look something like this
"cell1","cell2",1234,"more stuff"
"row2c1","row2c2",5678,"even more stuff"
Then when you open it in excel it should properly parse into cells
"cell1","cell2",1234,"more stuff"
"row2c1","row2c2",5678,"even more stuff"
Then when you open it in excel it should properly parse into cells
class
Try this csv class,may be helpful
http://www.phpclasses.org/browse/package/2480.html
http://www.phpclasses.org/browse/package/2480.html
If you've got PHP5, fputcsv() would work well
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.