欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel导出excel并设置单元格格式

laravel导出excel并设置单元格格式

2018年10月23日 10:09:39 浮华流年 阅读数:384                
$list = $this->recommend($request);
$data = array();
foreach($list as $k=>$v){
    $data[] = array_values($v);
    $len = count($v);
}
$header = array('日期','卖方','推荐人','推荐码','状态');
array_unshift($data,$header);
$filename = "推荐码";
$width = array('A'=>20,'B'=>20,'C'=>20,'D'=>30,'E'=>20);
excel($data,$filename,$len,$width);
               

 

 

if (!function_exists('excel')){
    /**
     * @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');
    }
}
           
来自  https://blog.csdn.net/m0_37971044/article/details/83302758
           
普通分类: