<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");
$sep = "\t";
$crlf = "\n";$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 $title . $crlf . $crlf;$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
for($i = 0; $i < $fields; $i++) {
    echo mysql_field_name($result, $i) . $sep;
}
echo $crlf;
while($row = mysql_fetch_row($result)) {
    $line = "";
    for($i = 0; $i<$fields; $i++) {
        $line .= $row[$i] . $sep;
    }
    echo $line . $crlf;
}
?>