用PHP将mysql数据表转换为excel文件格式
2024-07-24 12:56:31
供稿:网友
详细内容如下:
<?php
$db_server = "localhost";
$db_username = "mydowns";
$db_password = "";
$db_dbname = "mydowns";
$db_tblname = "user";
$connect = @mysql_connect($db_server, $db_username, $db_password)
or die("couldn't connect.");
$db = @mysql_select_db($db_dbname, $connect)
or die("couldn't select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("content-type: application/$file_type");
header("content-disposition: attachment; filename=mydowns.$file_ending");
header("pragma: no-cache");
header("expires: 0");
$now_date = date('y-m-d h:i');
$title = "数据库名:$db_dbname,数据表:$db_tblname,备份日期:$now_date";
$sql = "select * from $db_tblname";
$alt_db = @mysql_select_db($db_dbname, $connect)
or die("couldn't select database");
$result = @mysql_query($sql,$connect)
or die(mysql_error());
echo("$title/n");
$sep = "/t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "/t";
}
print("/n");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "null".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "/t";
print(trim($schema_insert));
print "/n";
$i++;
}
return (true);
?>本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。