Re: generate xls file on fly
| From: | Adrian Bruce | Date: | Fri, 08 Feb 2008 15:02:19 +0000 |
| Subject: | Re: generate xls file on fly | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268938@lists.php.net to get a copy of this message | ||
Hiep Nguyen wrote:
hi friends, i have a php page with the following logic: <html> <head> <title>Download</title> </head> <table> <tr><td>Title></td><td>Author</td></tr> <? $sql = "select title,author from book where title != null and author != null"; ?> <? $rs = mysql_query($sql) or die(mysql_error()); ?> <? while($row = mysql_fetch_array($rs)) { ?> <tr><td><?=$row[0];?></td><td><?=$row[1];?></td></tr> <? } ?> <tr><td><a href="">Download Into Excel File</a></td></tr> </table> </html> is there anyway to generate this into xls file w/o using fopen & fwrite to the server? my goal is to have a link after the table and user can click on that link and a save window pop up to allow user to save to local disk. appreciate very much for any input. t. hiepSomthing like this works ok, you can further refine it for the purpse you want. <?php $q= mysql_query("select * from table"); $count = mysql_num_fields($q); //print field names for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } //print data while($row = mysql_fetch_array($q, MYSQL_NUM)) { $table[] = implode("\t", $row); } $table= $header . "\r\n" . implode("\r\n", $table); //set headers that prompt user for download header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Disposition: attachment; filename=fileName"); echo $table; ?> Ade