PHP将数据库导出为excel表
?php /*连接数据库*/ $DB_Server = "localhost"; $DB_Username = "root"; $DB_Password = "123456"; $DB_DBName = "mydb"; //目标数据库名 $DB_TBLName = "mytable"; //目标表名 $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die(
/*连接数据库*/
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "123456";
$DB_DBName = "mydb"; //目标数据库名
$DB_TBLName = "mytable"; //目标表名
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
mysql_query("Set Names 'utf8'");
$savename = date("YmjHis"); //导出excel文件名
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=utf8");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");
/*写入备注信息*/
$now_date = date("Y-m-j H:i:s");
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
echo("$title\n");
/*查询数据库*/
$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());
/*写入表字段名*/
for ($i = 0; $i
echo mysql_field_name($result,$i) . ",";
}
echo "\n";
/*写入表数据*/
$sep = ",\t";
while($row = mysql_fetch_row($result)) {
$data = "";
for($i=0; $i
$data .= "NULL".$sep; //处理NULL字段
elseif ($row[$i] != "")
$data .= "$row[$i]".$sep;
else
$data .= "".$sep; //处理空字段
}
echo $data."\n";
}
?>
原文地址:PHP将数据库导出为excel表, 感谢原作者分享。
推荐阅读
-
用ASP将数据库中的数据直接导出到EXCEL表中
-
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
-
使用NPOI将Excel表导入到数据库中
-
解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误
-
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
-
php将数据库导出成excel的方法
-
ASP将access数据导出为excel电子表的方法
-
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
-
PHP导出Excel表
-
wpf 窗口程序下将datagrid导出为excel