php如何把mysql数据库导入到excel表格
展开全部
<?php
//需求:用php将mysql数据导入到excel中
//数据库配置信息
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "admin";
$DB_DBName = "shop";
$DB_TBLName = "sdb_widgets_set";
$savename = date("Y-m-j H:i:s");
// 数据库连接
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
//注意mysql 导入cxcel中的时候
mysql_query("Set Names 'gbk'");
//定义文件导出的格式
$file_type = "vnd.ms-excel";
//定义文件后缀名称
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=gbk");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");
$now_date = date("Y-m-j H:i:s");
//定义要输出的数据表标题
$title = "数据表名:$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;
else if ($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);
?>
//需求:用php将mysql数据导入到excel中
//数据库配置信息
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "admin";
$DB_DBName = "shop";
$DB_TBLName = "sdb_widgets_set";
$savename = date("Y-m-j H:i:s");
// 数据库连接
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
//注意mysql 导入cxcel中的时候
mysql_query("Set Names 'gbk'");
//定义文件导出的格式
$file_type = "vnd.ms-excel";
//定义文件后缀名称
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=gbk");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");
$now_date = date("Y-m-j H:i:s");
//定义要输出的数据表标题
$title = "数据表名:$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;
else if ($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);
?>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询