這篇文章主要介紹了關於php 通過html-table形式完成excel下載的功能實現,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
/** * @param $datas 具體資料 * @param $titles 列名 * @param $filename 檔案名稱 * @param $extraMessage 額外資訊 */public static function createExcel($datas, $titles, $filename, $extraMessage = ''){ $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; $str .= "<table border=1>"; //表頭 $str .= "<tr>"; foreach ($titles as $title) { $str .= "<td style='width: 60px;'>{$title}</tdw>"; } $str .= "</tr>\n"; //具體資料 foreach ($datas as $key => $rt) { $str .= "<tr>"; foreach ($rt as $k => $v) { $str .= "<td>{$v}</td>"; } $str .= "</tr>\n"; } //額外資訊 if (!empty($extraMessage)) { $str .= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='19' >{$extraMessage}</th></tr>"; } $str .= "</table></body></html>"; header("Content-Type: application/vnd.ms-excel; name='excel'"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=" . $filename); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/download");; header("Pragma: no-cache"); header("Expires: 0"); exit($str);}
最下方本次匯出合計為額外資訊
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!