转为真正的excel格式几乎是不可能的,即使是微软对这个也未弄好。
不过有可变通的方式。
<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");$conn = @mysql_connect("localhost", "root", "") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<table border="1" cellspacing="2" cellpadding="2" width="50%" align="center">';
// 输出标题
echo '<tr bgcolor="#cccccc"><td colspan="3" align="center">' . $title . '</td></tr>';$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
// 输出字段名
echo '<tr bgcolor="blue">';
for($i = 0; $i < $fields; $i++) {
    echo '<td>' . mysql_field_name($result, $i) . '</td>';
}
echo '</tr>';
// 输出内容
while($row = mysql_fetch_row($result)) {
    echo '<tr>';
    for($i = 0; $i<$fields; $i++) {
        echo '<td>' . $row[$i] . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

解决方案 »

  1.   

    http://202.115.130.242/6601/temp/test13.php
    没有提示下载的啊
      

  2.   

    要下载就加上这两句然后输出
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=" . $filename);
    ...这里是输出$filename为目标文件名,不含路径
      

  3.   

    下载得到test.xls
    内容为
    <table border="1" cellspacing="2" cellpadding="2" width="50%" align="center"><tr bgcolor="#cccccc"><td colspan="3" align="center">数据库名:test, 数据表:test, 备份日期:2003-03-21 18:17:36</td></tr><tr bgcolor="blue"><td>f1</td><td>f2</td></tr><tr><td>12456</td><td>12345</td></tr></table>