php 根据html table生成excel文件
1 <?php 2 3 /* 4 *处理Excel导出 5 *@param $datas array 设置表格数据 6 *@param $titlename string 设置head 7 *@param $title string 设置表头 8 */ 9 function excelData($datas, $titlename, $title, $filename) { 10 $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>"; 11 $str .= "<table border=1><head>" . $titlename . "</head>"; 12 $str .= $title; 13 foreach ($datas as $key => $rt) { 14 $str .= "<tr>"; 15 foreach ($rt as $k => $v) { 16 $str .= "<td>{$v}</td>"; 17 } 18 $str .= "</tr>\n"; 19 } 20 $str .= "</table></body></html>"; 21 echo $str; 22 header("Content-Type: application/vnd.ms-excel; name='excel'"); 23 header("Content-type: application/octet-stream"); 24 header("Content-Disposition: attachment; filename=" . $filename); 25 header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 26 header("Pragma: no-cache"); 27 header("Expires: 0"); 28 exit($str); 29 } 30 31 32 $dataResult = array(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); 33 $headTitle = "XX保险公司 优惠券赠送记录"; 34 $title = "优惠券记录"; 35 $headtitle = "<tr style='height:50px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>"; 36 $titlename = "<tr> 37 <th style='width:70px;' >合作商户</th> 38 <th style='width:70px;' >会员卡号</th> 39 <th style='width:70px;'>车主姓名</th> 40 <th style='width:150px;'>手机号</th> 41 <th style='width:70px;'>车牌号</th> 42 <th style='width:100px;'>优惠券类型</th> 43 <th style='width:70px;'>优惠券名称</th> 44 <th style='width:70px;'>优惠券面值</th> 45 <th style='width:70px;'>优惠券数量</th> 46 <th style='width:70px;'>赠送时间</th> 47 <th style='width:90px;'>截至有效期</th> 48 </tr>"; 49 $filename = $title . ".xls"; 50 excelData($dataResult, $titlename, $headtitle, $filename); 51 echo 'success';
每一天都是崭新的