error_reporting(E_ALL ^ E_DEPRECATED);
$localhost = '数据库地址';
$dbname = '数据库名';
$dbuser = '数据库账号';
$dbpwd = '数据库密码';
$tbname = "需要导出的数据库表名";
ob_end_clean();
header('Content-type: text/html; charset=utf-8');
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header("Content-Disposition:filename=data.xls");// 文件名自己改,默认data.xls
$conn = mysql_connect($localhost,$dbuser,$dbpwd) or die("连接数据库失败");
mysql_select_db($dbname, $conn);
mysql_query("set names 'UTF-8'");
$result=mysql_query("SELECT * FROM $tbname");
echo "
echo "
";echo "
A1单元格";echo "
B1单元格";echo "
C1单元格";echo "
D1单元格";echo "
";echo "
";while($row=mysql_fetch_array($result)){
//下面data改成你自己数据库对应的字段
$data1 = $row["data1"];
$data2 = $row["data2"];
$data3 = $row["data3"];
$data4 = $row["data4"];
//输出字段的数据到单元格
//下面data改成你自己数据库对应的字段
echo "
echo "
";echo "
$data1";echo "
$data2";echo "
$data3";echo "
$data4";echo "
";echo "
";}?>