一個php檔案就可以把資料庫的資料匯出Excel表格
阿新 • • 發佈:2018-12-28
資料庫內容太多,複製貼上太麻煩?
那就用程式碼實現把,把程式碼寫好了,匯出還不容易嗎,訪問即可匯出。
excel.php
<?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"]; //輸出欄位的資料到單元格 //下面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>"; } ?>
然後訪問excel.php就可以彈出表格下載了。
作者:TANKING
網站:likeyunba.com
微信:likeyunba520