Here's what I ended up doing that works:


(do all the db connection stuff - get your result, etc..)

<table width="100%" border="1">
    <td> <div align="center"><strong>Name</strong></div></td>
        <td> <div align="center"><strong>Company </strong></div></td>
        <td> <div align="center"><strong>Address </strong></div></td>
        <td> <div align="center"><strong>E-mail</strong></div></td>
        <td> <div align="center"><strong>Telephone</strong></div></td>

  while ($row = mysql_fetch_assoc($res)) {
        <td><?php echo $row[first] . $row[last]; ?></td>
        <td><?php echo $row[company] . $row[title]; ?></td>             
        <td><?php echo $row[address1] . "," . $row[city] . "," . $row[state] . "," . 
$row[zip]; ?></td>
        <td><?php echo $row[email]; ?></td>
        <td><?php echo $row[phone]; ?></td>


use the header() info in this post at the top of your page, create another page and 
link to this script. You can either view the excel or save to disk...


>>> Geir Pedersen - Activio AS <[EMAIL PROTECTED]> 03/24/04 08:04PM >>>


> I am looking for the easiest way to export data to an excel file. Is
> the eaiest way to use PHP's file handling functions?

I assume you want to delivery the execl file to a web user.
Then there is no need to write the data to disk, you can
generate the whole thing on the fly while you deliver it to the
user. Here is a basic description on how:

1) Create a PHP document to generate the execl file and make
   sure the script produces HTTP headers telling the user agent
   that it is receiving an excel document. Start the script

      header ("Content-type: application/");
      header ("Content-Disposition: attachment" );

   The content disposition header says that the execl file is
   to be stored on disk. 

2) Generate the spreadsheet data as a list of newline separated
   rows with tab separated fields:

   echo "field1\tfield2\t...\n";

3) Set up a link to the new PHP document. When a user clicks on
   that link, he will be prompted by his user agent on where to
   save the excel file.


Geir Pedersen 

PHP Database Mailing List (
To unsubscribe, visit: 

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to