laravel导出类

<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
/**
 * 导入导出
 * Class IndexController
 * @package App\Http\Controllers\Admin
 */
class ExcelController extends Controller{

    // 导出【我要报名列表】
    public function my(Request $request)
    {
        $field = Config::get("params.lingyu_type"); // 感兴趣领域
        $list = array();
        $res_trades = array();
        $res = DB::table("sign_up")
            ->where("type","my")
            ->orderBy("create_time","desc")
            ->get();
        foreach ($res as $k=>$v)
        {
            $res_trades[$k] = $v;
            $res_trades[$k]->info = unserialize($v->info);
        }

        $str = '';
        foreach ($res_trades as $k=>$v){
            foreach ($field as $k1=>$v1)
            {
                // 感兴趣领域
                if(strpos($v->info['field'],"$k1") !== false)
                {
                    $str .= $v1.',';
                }
            }
            $list[$k]['xuhao'] = $k+1;
            $list[$k]['username'] = $v->username;
            $list[$k]['phone'] = $v->phone;
            $list[$k]['email'] =  $v->email;
            $list[$k]['connect'] =  $v->info['connect'];
            $list[$k]['company'] =  $v->info['company'];
            $list[$k]['country'] =  $v->info['country'];
            $list[$k]['hy'] =  $v->info['hy'];
            $list[$k]['field'] =  !empty($str) ? rtrim($str,",") : '';
            $list[$k]['objective'] =  $v->info['objective'];
            $list[$k]['view'] =  $v->info['view'];
            $list[$k]['duijie'] =  $v->info['duijie'] == '0' ? '是' : '否';
            $list[$k]['create_time'] = date("Y-m-d H:i:s", $v->create_time);
        }

        $data = array();
        foreach($list as $k=>$v){
            $data[] = array_values($v);
            $len = count($v);
        }
        $header = array('序号','姓名','手机','Email','电话','公司/学校','国家','所属行业','感兴趣领域','参观目的','希望见到的品牌或企业名称','是否愿意组委会安排与意向见到的企业提前对接','报名时间');
        array_unshift($data,$header);
        $filename = "我要报名导出表";
        $width = array('A'=>10,'B'=>20,'C'=>20,'D'=>20,'E'=>30,'F'=>30,'G'=>20,'H'=>20,'I'=>20,'J'=>20,'K'=>40,'L'=>50,'M'=>20);
        $this->excel($data,$filename,$len,$width);
    }

    // 导出【志愿者报名列表】
    public function volunteer(Request $request)
    {
        $job = Config::get("params.job_type"); // 意向岗位
        $list = array();
        $res_trades = array();
        $res = DB::table("sign_up")
            ->where("type","volunteer")
            ->orderBy("create_time","desc")
            ->get();
        foreach ($res as $k=>$v)
        {
            $res_trades[$k] = $v;
            $res_trades[$k]->info = unserialize($v->info);
        }

        foreach ($res_trades as $k=>$v){
            $list[$k]['xuhao'] = $k+1;
            $list[$k]['username'] = $v->username;
            $list[$k]['age'] = $v->info['age'];
            $list[$k]['company'] =  $v->info['company'];
            $list[$k]['phone'] =  $v->phone;
            $list[$k]['email'] =  $v->email;
            $list[$k]['sex'] =  $v->info['sex'] == '0' ? '男' : '女';
            $list[$k]['current'] =  $v->info['current'] == '0' ? '学生' : '在职';
            $list[$k]['job'] =  isset($job[$v->info['job']]) ? $job[$v->info['job']] : '--';
            $list[$k]['create_time'] = date("Y-m-d H:i:s", $v->create_time);
        }

        $data = array();
        foreach($list as $k=>$v){
            $data[] = array_values($v);
            $len = count($v);
        }

        $header = array('序号','姓名','年龄','学校/公司','手机','邮箱','性别','目前状态','意向岗位','报名时间');
        array_unshift($data,$header);
        $filename = "志愿者报名导出表";
        $width = array('A'=>10,'B'=>20,'C'=>20,'D'=>20,'E'=>20,'F'=>20,'G'=>20,'H'=>20,'I'=>30,'J'=>20);
        $this->excel($data,$filename,$len,$width);
    }

    // 导出【活动报名列表】
    public function activity(Request $request)
    {
        $list = array();
        $res_trades = array();
        $res = DB::table("sign_up")
            ->where("type","activity")
            ->orderBy("create_time","desc")
            ->get();
        foreach ($res as $k=>$v)
        {
            $res_trades[$k] = $v;
            $res_trades[$k]->info = unserialize($v->info);
        }

        foreach ($res_trades as $k=>$v){
            $list[$k]['xuhao'] = $k+1;
            $list[$k]['username'] = $v->username;
            $list[$k]['phone'] =  $v->phone;
            $list[$k]['email'] =  $v->email;
            $list[$k]['activity'] =  $v->info['activity'];
            $list[$k]['create_time'] = date("Y-m-d H:i:s", $v->create_time);
        }

        $data = array();
        foreach($list as $k=>$v){
            $data[] = array_values($v);
            $len = count($v);
        }

        $header = array('序号','姓名','手机','邮箱','意向参与活动/论坛','报名时间');
        array_unshift($data,$header);
        $filename = "活动报名导出表";
        $width = array('A'=>10,'B'=>20,'C'=>20,'D'=>20,'E'=>20,'F'=>20);
        $this->excel($data,$filename,$len,$width);
    }

    /**
     * @desc 导出excel
     * @param $data 二维数组
     * @param $filename 导出excel的名称
     * @param $len 导出excel列的个数
     * @param $arr 每一列的长度数组
     */
    function excel($data,$filename,$len,$width){
        $filename = $filename.date('Y-m-d H:i:s',time());
        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        foreach($arr as $k=>$v){
            $length = $arr[$len-1];
        }
        $filename = $filename.date('Y-m-d',time());
        $fw='A1:'.$length.count($data);
        Excel::create(iconv('UTF-8', 'GBK', $filename),function ($excel) use ($data,$fw,$width){
            $f = $fw;
            $excel->sheet('recommend',function ($sheet) use ($data,$f,$width) {
                $sheet->rows($data);
                $sheet->setWidth($width);
                $sheet->cells($f,function($cells) {
                    $cells->setAlignment('left');
                });
            });
        })->export('xls');
    }

}
View Code

 

posted on 2019-08-19 21:23  shenzen_小白  阅读(266)  评论(0编辑  收藏  举报

导航