你直接输出xls 文件就可以了
Example;id name date
1 rongge 2004-09-20
2 admin 2004-09-21保存成a.xls 打开的时候就是excel文件了
注意:
保存的时候 每一个字段中间要一个空格分割,每一行数据之间要换行
Example;id name date
1 rongge 2004-09-20
2 admin 2004-09-21保存成a.xls 打开的时候就是excel文件了
注意:
保存的时候 每一个字段中间要一个空格分割,每一行数据之间要换行
<?php
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Content-type:application/vnd.ms-excel");
Header("Content-Disposition:attachment;filename=export_excel_gshjsl.xls"); $tx='表头';
echo $tx."\n\n";
//输出内容如下:
echo "姓名"."\t";
echo "年龄"."\t";
echo "学历"."\t";
echo "\n";
echo "张三"."\t";
echo "25"."\t";
echo "本科"."\t";
########################################
?>
// 转载请注明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;
}
?>