Re: exporting data to excel
| From: | Mignon Hunter | Date: | Thu, 25 Mar 2004 20:42:59 +0000 |
| Subject: | Re: exporting data to excel | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-33650@lists.php.net to get a copy of this message | ||
Here's what I ended up doing that works:
********************************
(do all the db connection stuff - get your result, etc..)
<table width="100%" border="1">
<tr>
<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>
</tr>
<?php
while ($row = mysql_fetch_assoc($res)) {
?>
<tr>
<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>
</tr>
<?php
}
?>
</table>
******************************
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...
Thanks
Mignon
>>> Geir Pedersen - Activio AS <geirp@activio.com> 03/24/04 08:04PM >>>
Matthew,
> 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
with:
header ("Content-type: application/vnd.ms-excel");
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
http://www.activio.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php