PHP原生EXCEL導出 經測試 帶樣式 無插件 無亂碼,不須要引入任何插件,不須要修改任何編碼php
(使用時只須要修改引入php數據庫配置文件、修改thead tbody中的數據便可、根據本身的須要去接收數據和生成數據。)html
代碼以下:mysql
<?php try { require_once("database.php"); } catch (Exception $e) { $message="數據庫文件鏈接失敗!請聯繫管理員!"; die($message); } $filename='test';//文件名 //設置頭 ob_end_clean(); header('Pragma: public'); header('Expires: 0'); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Content-Type:application/force-download'); header('Content-Type:application/vnd.ms-execl'); header('Content-Type:application/octet-stream'); header('Content-Type:application/download'); header('Content-Disposition:attachment;filename="'.$filename.'.xls"'); header('Content-Transfer-Encoding:binary'); //Excel頭 echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><!DOCTYPE html><html><head><meta http-equiv="Content-type" content="text/html;charset=UTF-8" /><style id="Classeur1_16681_Styles"></style></head><body><div id="Classeur1_16681" align=center x:publishsource="Excel"><table x:str border="1" cellpadding=0 cellspacing=0 style="font-family:Microsoft YaHei;text-align:center;border-collapse: collapse">' ; //導出什麼數據 //thead tbody開始 echo '<thead><tr><th class=xl2216681 nowrap>序號</th>'; echo '<th class=xl2216681 nowrap>標題1</th>'; echo '<th class=xl2216681 nowrap>標題2</th>'; echo '<th class=xl2216681 nowrap>標題3</th>'; echo '<th class=xl2216681 nowrap>標題4</th></tr></thead><tbody>'; //循環輸出內容 $i=0; $results=mysqli_query($sql_link,"select xx1,xx2,xx3,xx4 from xxx"); while ($rows=mysqli_fetch_array($results)) { $i++; echo '<tr><td class=xl2216681 nowrap>'.$i.'</td>'; echo '<td class=xl2216681 nowrap>'.$rows[0].'</td>'; echo '<td class=xl2216681 nowrap>'.$rows[1].'</td>'; echo '<td class=xl2216681 nowrap>'.$rows[2].'</td>'; echo '<td class=xl2216681 nowrap>'.$rows[3].'</td></tr>'; } //tbody結束 table結束 表格尾 echo '</tbody></table></div></body></html>'; ?>