YII2框架的excel表格導出

最近的項目作到關於表格輸出的功能,以前用TP的時候也作過,趁着此次功能比較多樣的機會整理一下php

本文是基於YII2框架進行開發的,不一樣框架可能會須要更改html

一.普通excel格式表格輸出

先是最普通的導出.xls格式的表格。首先先看一下表格在網站的顯示效果sql

clipboard.png

這裏能夠看到整個表格一共是7列。下面來看代碼的實現。數據庫

1.controller文件數組

//導出統計瀏覽器

public function actionStatistics(){
    //設置內存
    ini_set("memory_limit", "2048M");
    set_time_limit(0);

    //獲取用戶ID
    $id         =   Yii::$app->user->identity->getId();

    //去用戶表獲取用戶信息
    $user       =   Employee::find()->where(['id'=>$id])->one();

    //獲取傳過來的信息(時間,公司ID之類的,根據須要查詢資料生成表格)
    $params     =   Yii::$app->request->get();
    $objectPHPExcel = new \PHPExcel();

    //設置表格頭的輸出
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('A1', '代理公司');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('B1', '收入');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('C1', '成本');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('D1', '稿件數');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('E1', '毛利(收入-成本)');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('F1', '毛利率(毛利/收入)*100%');
    $objectPHPExcel->setActiveSheetIndex()->setCellValue('G1', 'ARPU值');

    //跳轉到recharge這個model文件的statistics方法去處理數據
    $data = Recharge::statistics($params);

    //指定開始輸出數據的行數
    $n = 2;
    foreach ($data as $v){
        $objectPHPExcel->getActiveSheet()->setCellValue('A'.($n) ,$v['company_name']);
        $objectPHPExcel->getActiveSheet()->setCellValue('B'.($n) ,$v['company_cost']);
        $objectPHPExcel->getActiveSheet()->setCellValue('C'.($n) ,$v['cost']);
        $objectPHPExcel->getActiveSheet()->setCellValue('D'.($n) ,$v['num']);
        $objectPHPExcel->getActiveSheet()->setCellValue('E'.($n) ,$v['gross_margin']);
        $objectPHPExcel->getActiveSheet()->setCellValue('F'.($n) ,$v['gross_profit_rate']);
        $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n) ,$v['arpu']);
        $n = $n +1;
    }
    ob_end_clean();
    ob_start();
    header('Content-Type : application/vnd.ms-excel');

    //設置輸出文件名及格式
    header('Content-Disposition:attachment;filename="代理公司統計'.date("YmdHis").'.xls"');

    //導出.xls格式的話使用Excel5,如果想導出.xlsx須要使用Excel2007
    $objWriter= \PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
    $objWriter->save('php://output');
    ob_end_flush();

    //清空數據緩存
    unset($data);
}

2.model文件緩存

<?php
    namespace app\models;//model層的命名空間
    //注意要引用yii的arrayhelper
    use yii\helpers\ArrayHelper;
    use Yii;
    class Recharge extends \yii\db\ActiveRecord
    {
        //excel一次導出條數
        const EXCEL_SIZE = 10000;
    
        //統計導出
        public static function statistics($params){

        //導出時間條件
        if(empty($params['min'])){
            $date_max = date("Y-m-d",strtotime("-1 day"));
            $date_min = date("Y-m-d",strtotime("-31 day"));
        }else{
            $date_min = $params['min'];
            $date_max = $params['max'];
        }
        $where = '';
        $where .= '(`issue_date` BETWEEN '.'\''.$date_min.'\''.' AND '.'\''.$date_max.'\')';

        //查找指定數據
        $sql = 'select
                article.company_id,
                article.cost,
                article.company_cost
                from article WHERE article.status=2 AND '.$where;
        $article = Article::findBySql($sql)->asArray()->all();
        $article = ArrayHelper::index($article,null,'company_id');
        $companys = [];

        foreach ($article as $key=>$v){
            if(empty($key)){
                continue;
            }else{
                $number         =   count($v);
                $company        =   Company::find()->where(['id'=>$key])->select('name')->one();
                $company_name   =   $company['name'];
                $cost           =   0;
                $company_cost   =   0;
                foreach ($v as $n){
                    $cost += $n['cost'];
                    $company_cost += $n['company_cost'];
                }
                if($company_cost == 0){
                    $company_cost =1;
                }

                //這裏注意,數據的存儲順序要和輸出的表格裏的順序同樣
                $companys[] = [
                    //公司名
                    'company_name'      =>  $company_name,

                    //收入
                    'company_cost'      =>  $company_cost,

                    //成本
                    'cost'              =>  $cost,

                    //稿件數
                    'num'               =>  $number,

                    //毛利
                    'gross_margin'      =>  $company_cost-$cost,

                    //毛利率
                    'gross_profit_rate' =>  round(($company_cost-$cost)/$company_cost*100,2).'%',

                    //ARPU值
                    'arpu'              =>  round($company_cost/$number,2),
                ];
            }
        }
        return $companys;
    }
}

最終導出的效果(單元格大小導出後調整過)能夠看到和網頁顯示的基本同樣。app

clipboard.png

二.大數據表格導出

這時老闆說了,咱們不能只看總和的數據,最好是把詳細數據也給導出來。既然老闆發話了,那就作吧。仍是按照第一種的方法去作,結果提示我php崩潰了,再試一次發現提示寫入字節超出。打開php的配置文件php.ini框架

memory_limit = 128M

發現默認內存已經給到128M,應該是足夠的了。因而我打開數據庫一看,嚯!yii

接近83萬條的數據進行查詢並導出,可不是會出問題嘛!怎麼辦呢,因而我Google了一下,發現對於大數據(2萬條以上)的導出,最好是以.csv的形式。不說廢話,直接上代碼

1.controller文件

//導出清單

public function actionInventory(){
    ini_set("memory_limit", "2048M");
    set_time_limit(0);
    $id         =   Yii::$app->user->identity->getId();
    $user       =   Employee::find()->where(['id'=>$id])->one();
    $params     =   Yii::$app->request->get();
    
    //相似的,跳轉到recharge這個model文件裏的inventory方法去處理數據
    $data       =   Recharge::inventory($params);
    
    //設置導出的文件名
    $fileName   =   iconv('utf-8', 'gbk', '代理商統計清單'.date("Y-m-d"));
    
    //設置表頭
    $headlist   =   array('代理商','文章ID','文章標題','媒體','統計時間範圍','狀態','建立時間','審覈時間','發稿時間','退稿時間','財務狀態','成本','銷售額','是不是預收款媒體類型','訂單類別');
    header('Content-Type: application/vnd.ms-excel');
    
    //指明導出的格式
    header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
    header('Cache-Control: max-age=0');
    
    //打開PHP文件句柄,php://output 表示直接輸出到瀏覽器
    $fp = fopen('php://output', 'a');
    
    //輸出Excel列名信息
    foreach ($headlist as $key => $value) {
        //CSV的Excel支持GBK編碼,必定要轉換,不然亂碼
        $headlist[$key] = iconv('utf-8', 'gbk', $value);
    }
    
    //將數據經過fputcsv寫到文件句柄
    fputcsv($fp, $headlist);
    
    //每隔$limit行,刷新一下輸出buffer,不要太大,也不要過小
    $limit = 100000;
    
    //逐行取出數據,不浪費內存
    foreach ($data as $k => $v) {
        //刷新一下輸出buffer,防止因爲數據過多形成問題
        if ($k % $limit == 0 && $k!=0) {
            ob_flush();
            flush();
        }
        $row = $data[$k];
        foreach ($row as $key => $value) {
            $row[$key] = iconv('utf-8', 'gbk', $value);
        }
        fputcsv($fp, $row);
    }
}

2.model文件(由於這部分我要處理的過多,因此只選擇了部分代碼),在查詢數據那部分,由於要查的數據較多,因此能夠結合我以前寫的關於Mysql大數據查詢處理的文章看一下

//清單導出

public static function inventory($params){
    //統計時間範圍
    if(!empty($params['min']) && !empty($params['max'])){
        $ti = strtotime($params['max'])+3600*24;
        $max = date('Y-m-d',$ti);
        $time = $params['min'].'-'.$params['max'];
        $date_min = $params['min'];
        $date_max = $max;
    }else{
        $date_max = date('Y-m-d');
        $date_min = date('Y-m-d',strtotime("-31 day"));
        $time = $date_min.'-'.$date_max;
    }
    //查詢數據
    if($params['state'] == 1){
        $where  = '';
        $where .= ' AND (`issue_date` BETWEEN '.'\''.$date_min.'\''.' AND '.'\''.$date_max.'\')';
        $map = 'select
                 company.name,
                 article.id,
                 article.title,
                 media.media_name,
                 article.status,
                 article.created,
                 article.audit_at,
                 article.issue_date,
                 article.back_date,
                 article.finance_status,
                 article.cost,
                 article.company_cost,
                 media.is_advance
                 from article
                    LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
                    LEFT JOIN `order`        ON custom_package.order_id = `order`.`id`
                    LEFT JOIN company        ON company.id = article.company_id
                    LEFT JOIN media          ON media.id = article.media_id
                    where article.status=2   and `order`.package=0'.$where;
        //查找的第一部分數據,使用asArray方法可使咱們查找的結果直接造成數組的形式,沒有其餘多餘的數據佔空間(注意:我這裏查找分三部分是由於我要查三種不一樣的數據)
        $list1   = Article::findBySql($map)->asArray()->all();
        $where2  = '';
        $where2 .= ' AND (`issue_date` BETWEEN '.'\''.$date_min.'\''.' AND '.'\''.$date_max.'\')';
        $where2 .= ' AND (`back_date` > \''.$date_max.'\')';
        $map2 = 'select
                 company.name,
                 article.id,
                 article.title,
                 media.media_name,
                 article.status,
                 article.created,
                 article.audit_at,
                 article.issue_date,
                 article.back_date,
                 article.finance_status,
                 article.cost,
                 article.company_cost,
                 media.is_advance
                 from article
                    LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
                    LEFT JOIN `order`        ON custom_package.order_id = `order`.`id`
                    LEFT JOIN company        ON company.id = article.company_id
                    LEFT JOIN media          ON media.id = article.media_id
                    where article.status=3   and `order`.package=0 '.$where2;
        //查找的第二部分數據
        $list2 = Article::findBySql($map2)->asArray()->all();
        $where3 = '';
        $where3 .= ' AND (`issue_date` BETWEEN '.'\''.$date_min.'\''.' AND '.'\''.$date_max.'\')';
        $map3 = 'select
                 company.name,
                 article.id,
                 article.title,
                 media.media_name,
                 article.status,
                 article.created,
                 article.audit_at,
                 article.issue_date,
                 article.back_date,
                 article.finance_status,
                 article.cost,
                 article.company_cost,
                 media.is_advance
                 from article
                    LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
                    LEFT JOIN `order`        ON custom_package.order_id = `order`.`id`
                    LEFT JOIN company        ON company.id = article.company_id
                    LEFT JOIN media          ON media.id = article.media_id
                    where article.status=5 '.$where3;
        //查找的第三部分數據
        $list3 = Article::findBySql($map3)->asArray()->all();
        $list4 = ArrayHelper::merge($list1,$list2);
        $list = ArrayHelper::merge($list4,$list3);
    }
    //把結果按照顯示順序存到返回的數組中
    if(!empty($list)){
        foreach ($list as $key => $value){
            //代理公司
            $inventory[$key]['company_name']    =   $value['name'];
            //文章ID
            $inventory[$key]['id']              =   $value['id'];
            //文章標題
            $inventory[$key]['title']           =   $value['title'];
            //媒體
            $inventory[$key]['media']           =   $value['media_name'];
            //統計時間
            $inventory[$key]['time']            =   $time;
            //狀態
            switch($value['status']){
                case 2:
                    $inventory[$key]['status']  = '已發佈';
                    break;
                case 3:
                    $inventory[$key]['status']  = '已退稿';
                    break;
                case 5:
                    $inventory[$key]['status']  = '異常稿件';
                    break;
            }
            //建立時間
            $inventory[$key]['created']         =   $value['created'];
            //審覈時間
            $inventory[$key]['audit']           = $value['audit_at'];
            //發稿時間
            $inventory[$key]['issue_date']      = $value['issue_date'];
            //退稿時間
            $inventory[$key]['back_date']       = $value['back_date'];
            //財務狀態
            switch($value['finance_status']){
                case 0:
                    $inventory[$key]['finance_status'] = '未到結算期';
                    break;
                case 1:
                    $inventory[$key]['finance_status'] = '可結算';
                    break;
                case 2:
                    $inventory[$key]['finance_status'] = '資源審批中';
                    break;
                case 3:
                    $inventory[$key]['finance_status'] = '財務審批中';
                    break;
                case 4:
                    $inventory[$key]['finance_status'] = '已結款';
                    break;
                case 5:
                    $inventory[$key]['finance_status'] = '未經過';
                    break;
                case 6:
                    $inventory[$key]['finance_status'] = '財務已審批';
                    break;
            }
            //成本
            $inventory[$key]['cost']            = $value['cost'];
            //銷售額
            $inventory[$key]['company_cost']    = $value['company_cost'];
            //是不是預售
            switch($value['is_advance']){
                case 0:
                    $inventory[$key]['is_advance']   = '否';
                    break;
                case 1:
                    $inventory[$key]['is_advance']   = '是';
                    break;
                case 2:
                    $inventory[$key]['is_advance']   = '合同';
                    break;
            }
            //訂單類別
            switch($params['state']){
                case 1:
                    $inventory[$key]['order_type'] = '時間區間無退稿完成訂單';
                    break;
                case 2:
                    $inventory[$key]['order_type'] = '時間區間發佈前退稿訂單';
                    break;
                case 3:
                    $inventory[$key]['order_type'] = '時間區間發佈後時間區間退稿訂單';
                    break;
                case 4:
                    $inventory[$key]['order_type'] = '時間區間以前發佈時間區間內退稿訂單';
                    break;
                case 5:
                    $inventory[$key]['order_type'] = '異常訂單';
                    break;
            }
        }
    }else{
        $inventory[0]['company_name']    =   '無數據導出';
    }
    return $inventory;
}

3.導出結果

clipboard.png

導出數量

clipboard.png

導出的文件

clipboard.png

基本上能夠保證整個過程在2~4秒內處理完成

三.合併單元格

老闆一看作的不錯,說你順便把充值統計的導出也作了把,想一想我都是處理過這麼多數據的人了,還不是分分鐘搞定的事?來,上原型圖

clipboard.png

噗,一口老血,話都說了,搞吧。在作的時候我發現,此次的導出主要是要解決單元格合併的問題。通過查資料發現,PHP自己是實現不了單元格合併的,因而我打算經過phpexcel來實現

若是是使用PHPExcel的話,基本操做是這樣的(合併A1到E1)

$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
// 表格填充內容
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');

結果

clipboard.png

或者這樣的(合併A1到E4)

$objPHPExcel->getActiveSheet()->mergeCells('A1:E4');
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');

結果

clipboard.png

這樣並不能知足個人要求,首先它是一個一個合併的,其次我要顯示的充值金額下面的類型是會變化的,不可能固定寫死,而後每次都更改。因此放棄了這種方法。

後來在小夥伴的幫助下嘗試用html轉存excel的方法

1.方法文件(由於我要天天定時執行,因此並無寫到controller層)

public function actionExcelRechargeStatistics(){

//先定義一個excel文件
    $filename   =   date('【充值統計表】('.date('Y-m-d').'導出)').".xls";
    header("Content-Type: application/vnd.ms-execl");
    header("Content-Type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
    //時間條件
    if(empty($params['min'])){
        $time       =   date('Y-m-d',strtotime("+1 day"));
        $where      =   ' created < \' '.$time.'\'';
    }else{
        $time       =   $params['min']+3600*24;
        $time_end   =   $params['max']+3600*24;
        $where      =   ' created <= \' '.$time_end.'\' AND created >= \''.$time.'\' ';
    }
    //充值類型列表
    $recharge_type  =   Recharge::find()->asArray()->all();
    if(empty($recharge_type)){
        $rechargelist[0]=   '';
    }else{
        $rechargelist   =   ArrayHelper::map($recharge_type,'id','recharge_name');
    }
    $rechargelist1      =   $rechargelist;
    $count              =   count($rechargelist1);
    //使用html語句生成顯示的格式
    $excel_content      =   '<meta http-equiv="content-type" content="application/ms-excel; charset=utf-8"/>';
    $excel_content     .=   '<table border="1" style="font-size:14px;">';
    $excel_content     .=   '<thead>
                                 <tr>
                                    <th rowspan="2">ID</th>
                                    <th rowspan="2">公司名稱</th>
                                    <th colspan='.$count.'>充值金額</th>
                                    <th rowspan="2">充值大小</th>
                                    <th rowspan="2">實際消費</th>
                                    <th rowspan="2">當前餘額</th>
                                 </tr>
                                 <tr>
                             ';
    foreach ($rechargelist1 as $v => $t){
            $excel_content     .=   '<th colspan="1">'.$t.'</th>';
    }
    $excel_content     .=   '</tr>
                             </thead>';
    //查找最新的固化數據
    $search = RechargeStatistics::find()->where($where)->asArray()->all();
    if(!empty($search)){
        foreach ($search as $key => $value){
            $search[$key]['recharge'] = unserialize($value['recharge']);
        }
    }
    //html語句填充數據
    if(empty($search)){
    }else{
        foreach ($search as $k) {
            $excel_content  .= '<td>'.$k['company_id'].'</td>';
            $excel_content  .= '<td>'.$k['company_name'].'</td>';
            foreach ($rechargelist1 as $v=>$t){
                $price = 0;
                foreach ($k['recharge'] as $q=>$w){
                    if($w['recharge_id'] == $v){
                        $price = $w['price'];
                        break;
                    }
                }
                $excel_content  .= '<td>'.$price.'</td>';
            }
            $excel_content  .= '<td>'.$k['total'].'</td>';
            $excel_content  .= '<td>'.$k['consume'].'</td>';
            $excel_content  .= '<td>'.($k['total']-$k['consume']).'</td></tr>';
        }
    }
    $excel_content  .=  '</table>';
    echo $excel_content;
    die;

}

2.結果

clipboard.png

到這裏基本就完成全部的任務了!

相關文章
相關標籤/搜索