Page 1 of 1

how to write data into csv(excel) file?

Posted: Thu Sep 13, 2007 4:18 am
by kiko
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.

Posted: Thu Sep 13, 2007 6:40 am
by jeffery
The manual has an example:

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;
    }
You need to use the "Append" mode

http://php.net/fwrite

cheers,
Jeffery

Posted: Thu Sep 13, 2007 7:40 am
by Begby
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

class

Posted: Thu Sep 13, 2007 7:42 am
by youscript
Try this csv class,may be helpful
http://www.phpclasses.org/browse/package/2480.html

Posted: Thu Sep 13, 2007 11:53 am
by pickle
If you've got PHP5, fputcsv() would work well