how to write data into csv(excel) file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kiko
Forum Newbie
Posts: 23
Joined: Fri Sep 07, 2007 6:42 am

how to write data into csv(excel) file?

Post 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.
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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
youscript
Forum Newbie
Posts: 10
Joined: Thu Sep 13, 2007 3:22 am

class

Post by youscript »

Try this csv class,may be helpful
http://www.phpclasses.org/browse/package/2480.html
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

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.
Post Reply