Home  >  Article  >  Backend Development  >  How to export excel table with php (code)

How to export excel table with php (code)

不言
不言forward
2018-09-28 15:49:119841browse

The content of this article is about sharing the method (code) of exporting excel tables with PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There is a lot of list data in the background of the website, and there is often a need to export excel tables. Let me share with you a practical method of exporting excel tables;

Not much to say, let’s get to the code;

/**
     * @param array $data 要导出的数据
     * @param array $title excel表格的表头
     * @param string $filename 文件名
     */
    public function daochu_excel($data=array(),$title=array(),$filename='报表'){//导出excel表格
        //处理中文文件名
        ob_end_clean();
        Header('content-Type:application/vnd.ms-excel;charset=utf-8');
    
        header("Content-Disposition:attachment;filename=export_data.xls");
        //处理中文文件名
        $ua = $_SERVER["HTTP_USER_AGENT"];
    
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        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') {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
        }else {
            header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
        }
        header ( "Content-type:application/vnd.ms-excel" );
    
    
        $html = "<!DOCTYPE html PUBLIC &#39;-//W3C//DTD XHTML 1.0 Transitional//EN&#39; &#39;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#39;>
            <html xmlns=&#39;http://www.w3.org/1999/xhtml&#39;>
            <meta http-equiv=&#39;Content-type&#39; content=&#39;text/html;charset=UTF-8&#39; />
            <head>
    
            <title>".$filename."</title>
            <style>
            td{
                text-align:center;
                font-size:12px;
                font-family:Arial, Helvetica, sans-serif;
                border:#1C7A80 1px solid;
                color:#152122;
                width:auto;
            }
            table,tr{
                border-style:none;
            }
            .title{
                background:#7DDCF0;
                color:#FFFFFF;
                font-weight:bold;
            }
            </style>
            </head>
            <body>
            <table width=&#39;100%&#39; border=&#39;1&#39;>
              <tr>";
        foreach($title as $k=>$v){
            $html .= " <td class=&#39;title&#39; style=&#39;text-align:center;&#39;>".$v."</td>";
        }
    
        $html .= "</tr>";
    
        foreach ($data as $key => $value) {
            $html .= "<tr>";
            foreach($value as $aa){
                $html .= "<td>".$aa."</td>";
            }
    
            $html .= "</tr>";
    
        }
        $html .= "</table></body></html>";
        echo $html;
        exit;
    }
The data of the

$title parameter is a one-dimensional array, as follows:
How to export excel table with php (code)

The $data parameter is a two-dimensional array, as follows:

How to export excel table with php (code)

Calling method:

$daochuData = DB::table(&#39;scholarship_to_weixin as s&#39;)->leftJoin(&#39;users as u&#39;,&#39;s.uid&#39;,&#39;=&#39;,&#39;u.id&#39;)
                ->leftJoin(&#39;admin as a&#39;,&#39;a.id&#39;,&#39;=&#39;,&#39;s.tx_checkid&#39;)
                ->orderBy(&#39;s.times&#39;,&#39;desc&#39;)
                ->select(&#39;s.*&#39;,&#39;u.nickname&#39;,&#39;u.tel&#39;,&#39;u.id as u_id&#39;,&#39;a.name as a_name&#39;,&#39;u.admin_beizhu_name&#39;)
                ->get();
            $title = array(&#39;序号&#39;,&#39;申请时间&#39;,&#39;申请人&#39;,&#39;备注名称&#39;,&#39;申请人手机号&#39;,&#39;提现金额&#39;,&#39;操作时间&#39;,&#39;操作人&#39;);
            $arr = [];
            foreach($daochuData as $k=>$v){
                $arr[] = array(
                        $k+1,
                        $v->times,
                        $v->nickname,
                        $v->admin_beizhu_name,
                        $v->tel,
                        $v->money,
                        $v->s_times,
                        $v->a_name
                );
            }
            
            $this->daochu_excel($arr,$title,&#39;红包提现到微信记录&#39;);

Result:
How to export excel table with php (code)Hope this is helpful to you.

The above is the detailed content of How to export excel table with php (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete