<?php 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 "<table>"; echo "<tr>"; echo "<th>A1单元格</th>"; echo "<th>B1单元格</th>"; echo "<th>C1单元格</th>"; echo "<th>D1单元格</th>"; echo "</tr>"; echo "</table>"; while($row=mysql_fetch_array($result)){ //下面data改成你自己数据库对应的字段 $data1 = $row["data1"]; $data2 = $row["data2"]; $data3 = $row["data3"]; $data4 = $row["data4"]; //输出字段的数据到单元格,www.3qpd.com //下面data改成你自己数据库对应的字段 echo "<table>"; echo "<tr>"; echo "<td>$data1</td>"; echo "<td>$data2</td>"; echo "<td>$data3</td>"; echo "<td>$data4</td>"; echo "</tr>"; echo "</table>"; } ?>本文转载至鸿盟创客https://www.3qpd.com/read-470-1.html