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. hiep


Somthing 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to