当前位置: 移动技术网 > IT编程>开发语言>PHP > php导出excel表格的使用

php导出excel表格的使用

2018年09月28日  | 移动技术网IT编程  | 我要评论

网站后台有很多列表数据,常常都会有导出excel表格的需求,和大家分享一个实用的导出excel表格方法;

不多说,上代码;

 1  /**
 2      * @param array $data 要导出的数据
 3      * @param array $title excel表格的表头
 4      * @param string $filename 文件名
 5      */
 6     public function daochu_excel($data=array(),$title=array(),$filename='报表'){//导出excel表格
 7         //处理中文文件名
 8         ob_end_clean();
 9         header('content-type:application/vnd.ms-excel;charset=utf-8');
10     
11         header("content-disposition:attachment;filename=export_data.xls");
12         //处理中文文件名
13         $ua = $_server["http_user_agent"];
14     
15         $encoded_filename = urlencode($filename);
16         $encoded_filename = str_replace("+", "%20", $encoded_filename);
17         if (preg_match("/msie/", $ua) || preg_match("/lcte/", $ua) || $ua == 'mozilla/5.0 (windows nt 6.1; trident/7.0; rv:11.0) like gecko') {
18             header('content-disposition: attachment; filename="' . $encoded_filename . '.xls"');
19         }else {
20             header('content-disposition: attachment; filename="' . $filename . '.xls"');
21         }
22         header ( "content-type:application/vnd.ms-excel" );
23     
24     
25         $html = "<!doctype html public '-//w3c//dtd xhtml 1.0 transitional//en' 'http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd'>
26             <html xmlns='http://www.w3.org/1999/xhtml'>
27             <meta http-equiv='content-type' content='text/html;charset=utf-8' />
28             <head>
29     
30             <title>".$filename."</title>
31             <style>
32             td{
33                 text-align:center;
34                 font-size:12px;
35                 font-family:arial, helvetica, sans-serif;
36                 border:#1c7a80 1px solid;
37                 color:#152122;
38                 width:auto;
39             }
40             table,tr{
41                 border-style:none;
42             }
43             .title{
44                 background:#7ddcf0;
45                 color:#ffffff;
46                 font-weight:bold;
47             }
48             </style>
49             </head>
50             <body>
51             <table width='100%' border='1'>
52               <tr>";
53         foreach($title as $k=>$v){
54             $html .= " <td class='title' style='text-align:center;'>".$v."</td>";
55         }
56     
57         $html .= "</tr>";
58     
59         foreach ($data as $key => $value) {
60             $html .= "<tr>";
61             foreach($value as $aa){
62                 $html .= "<td>".$aa."</td>";
63             }
64     
65             $html .= "</tr>";
66     
67         }
68         $html .= "</table></body></html>";
69         echo $html;
70         exit;
71     }

$title参数的数据是一个一维数组,如下:
$data参数是一个二维数组,如下:

 

调用方法:

 1 $daochudata = db::table('scholarship_to_weixin as s')->leftjoin('users as u','s.uid','=','u.id')
 2                 ->leftjoin('admin as a','a.id','=','s.tx_checkid')
 3                 ->orderby('s.times','desc')
 4                 ->select('s.*','u.nickname','u.tel','u.id as u_id','a.name as a_name','u.admin_beizhu_name')
 5                 ->get();
 6 
 7             $title = array('序号','申请时间','申请人','备注名称','申请人手机号','提现金额','操作时间','操作人');
 8 
 9             $arr = [];
10             foreach($daochudata as $k=>$v){
11                 $arr[] = array(
12                         $k+1,
13                         $v->times,
14                         $v->nickname,
15                         $v->admin_beizhu_name,
16                         $v->tel,
17                         $v->money,
18                         $v->s_times,
19                         $v->a_name
20                 );
21             }
22             
23             $this->daochu_excel($arr,$title,'红包提现到微信记录');

 

结果:

 

希望对您有帮助。谢谢!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网