PHP原生EXCEL导出 经测试 带样式 无插件 无乱码,不需要引入任何插件,不需要修改任何编码
(使用时只需要修改引入php数据库配置文件、修改thead tbody中的数据即可、根据自己的需要去接收数据和生成数据。)
代码如下:
<?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>';
?>