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';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM