Link to home
Start Free TrialLog in
Avatar of unreal400
unreal400

asked on

Creating Excel Files with PHP

I want to be able to create excel files with my php code and have the excel file saved to a location on the server.
I won't want it opening into excelon the users side I just want to create the excel file on the server side and have it stored somewhere.

Does anyone know of a tutorial on how to do this?

Kelly

Avatar of pjargon
pjargon

I looked into doing that for a little while, it didn't really work
If you're just handling data, why not just make a  csv?  Excel can read csv just fine (comma separated values)
It's possible, but as pjargon says, seems prone to errors.  If you want to try it anyway, here's a couple places to look:

http://jaxn.org/cataBlog.php?articleID=290
http://www.devshed.com/c/a/PHP/PHP-and-COM/1/
Avatar of unreal400

ASKER

Whats the easiest way to write out the files to the server.  I've never worked with creating files. just creating the webpages with php.  Can you point me to an example.
Should be pretty simple file output, yes?
thx google:  http://www.softwareforeducation.com/php/13file-io.htm

You may have to mess with permissions
I have found the easiest is a TAB delimted file.
Since sometimes values need to contain a ','
if you save everything into arrays, you can then write them to a file using serialize

http://www.php.net/serialize
http://www.php.net/unserialize

and read them using unserialize
About writing to a file - directly from php.net help on fwrite():
http://www.php.net/manual/en/function.fwrite.php

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // 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')) {
        print "Cannot open file ($filename)";
        exit;
   }

   // Write $somecontent to our opened file.
   if (!fwrite($handle, $somecontent)) {
       print "Cannot write to file ($filename)";
       exit;
   }
   
   print "Success, wrote ($somecontent) to file ($filename)";
   
   fclose($handle);
                   
} else {
   print "The file $filename is not writable";
}
?>
Avatar of Marcus Bointon
Commas inside fields is not a problem with CSV, because field content is usually quoted, thus escaping the commas. In some instances it will even allow line breaks inside fields. CSV for excel works very nicely as long as you're not doing anything that's excel specific, like drawing graphs or running macros, and is very simple to do. I generate Excel-tagged CSV for downloads - the important thing is to give it the correct MIME type for it to be picked up by Excel, which is application/msexcel.
ASKER CERTIFIED SOLUTION
Avatar of laurenty
laurenty

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial