zoukankan      html  css  js  c++  java
  • php导出excel表格的使用

    网站后台有很多列表数据,常常都会有导出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                 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,'红包提现到微信记录');

    结果:

    希望对您有帮助。谢谢!
  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/koxi/p/9703184.html
Copyright © 2011-2022 走看看