Forum Moderators: coopster & phranque

Message Too Old, No Replies

Form data to Excel

Converting Form data to an Excel file

         

balinor

5:50 pm on Apr 9, 2003 (gmt 0)

10+ Year Member



Hi all...I'm trying to find a good solution to the following question:

My client wants to have users fill out a form on their web site that will compile as an Excel file. I saw an earlier post about how to do this with ASP, but I am running the site on an Apache server so I will need either a PHP method or perhaps Javascript? I know nothing about PHP, but need to learn it eventually. I don't want to simply be handed the code, I would like to understand the logistics of the process as well. Anything you guys can provide would be most helpful. Thanks in advance!

Regards,
Padraic Ryan

jatar_k

6:38 pm on Apr 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld balinor,

I don't want to simply be handed the code

Now that is a good thing to hear. :)

Well, since excel reads comme delimited files you could store it that way. Excel has no problems opening them and php has no problems writing them.

The logic of the whole thing would be

You have a form that uses the POST method and the action is set to a php script.

That php script would read/validate the information from the $_POST [php.net] associative array and if it validates it would open a pointer to the .csv file where you are storing the data using fopen [php.net].

It would then put the data from the form into the structure it wants to store in the file using concatenation [php.net] or maybe using sprintf [php.net]

It then writes the line to the file using fwrite [php.net].

then close the file pointer using fclose [php.net].

Then it may either echo [php.net] some confirmation message or send them off to another page using header [php.net]

balinor

7:27 pm on Apr 9, 2003 (gmt 0)

10+ Year Member



Thanks jatar_k, that is exactly what I was looking for. Seems like a pretty simple concept which I actually understand for a change! Implementing it via code will be a whole different challenge, but one I am excited to try. I may be posting some follow ups to this as I go along. Thanks again!

-Padraic

RonPK

2:39 pm on Apr 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello balinor,
Another way to achieve your goal might be to
* write the posted data as an html-table into a file with the extension .xls (try this on your pc: Excel will open the file without any problems or import-worries)
* fopen() the file again
* determine the filesize with filesize()
* send some headers to make browsers think you're really sending an Excel-file:
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $fileName);
header("Content-length: ". $fileSize);
header("Content-Transfer-Encoding: BINARY");
* echo the data to the browser:
echo fread($fp, $fileSize); // fp is the filepointer
* close the file, fclose().
* if necessary, unlink the .xls-file

I'm using this in several sites, and it works fine in all modern browsers.