laravel 使用excel 设计表格的样式

首先是对excel 插件的使用 不在重复
传送门 :Laravel 5 中使用 Laravel Excel
当然少不了 excel 的: 官方文档
下面还是硬道理 直接撸代码

if ($res->code == 1) {

            if($query['excel'] == 'Y'){
                if(!($res->results)){
                    return redirect()
                        ->back()
                        ->withErrors('数据为空,请检查报表条件~')
                        ->withInput();
                }

                $arr[] = ['ID','用户ID','用户名称','数据ID','状态','金额(元)','创建日期'];
                $amount = 0.00 ;
                $total = count($res->results);

                foreach($res->results as $val){
                    $arr[] =[$val->id,$val->u_id,$val->username,$val->d_id,$val->type,$val->amount,date('Y-m-d',$val->created_at)];
                    $amount += $val->amount;
                }

                $arr[] = ['','','','','总额:',$amount];
                $arr[] = ['',' ',' ','',''];
                $arr[] = ['','总条数',$total,'',''];
                $arr[] = ['','总金额',$amount,'备注:',''];
                $arr[] = ['','用户注册','','','徒弟收益(11001)',''];
                $arr[] = ['','点击任务','','','师傅收益(11002)',''];
                $arr[] = ['','任务大厅','','','师爷收益(11003)',''];
                $arr[] = ['','徒弟收益','','','代理收益(11004)',''];
                $arr[] = ['','徒孙收益','','','股东收益(11005)',''];
                $arr[] = ['','代理收益','','',''];
                $arr[] = ['','股东收益','','',''];

                $arr[] = ['','','','',''];
                $arr[] = ['','报表用户',$query['username']];
                $arr[] = ['','报表日期',$query['start_date'].'至'.$query['end_date'],];
                $arr[] = ['','报表类型:'.$query['type'],'','',''];

                $cellData =  $arr;

                Excel::create('财务报表',function($excel) use ($cellData){

                    $excel->sheet('财务报表', function($sheet) use ($cellData){
                        $tot = count($cellData) ;
                        $sheet->setWidth(array(
                            'A'     =>  12,
                            'B'     =>  12,
                            'C'     =>  12,
                            'D'     =>  12,
                            'E'     =>  12,
                            'F'     =>  12,
                            'G'     =>  12,
                        ))->rows($cellData)->setFontSize(12);
//
//                        $sheet->row($tot-13, function($row) {
//                            $row->setBackground('#87eabd');
//                        });

                        // 数据内容主题 左对齐
                        $sheet->cells('A1:F'.$tot, function($cells) {
                            $cells->setAlignment('left');
                        });
                        // 菜单 样式
                        $sheet->cells('A1:G1', function($cells) {
                            $cells->setAlignment('center');
                            $cells->setFontWeight('bold');
                        });
                        // 数据统计 样式
                        $sheet->cells('A'.($tot-13).':F'.$tot, function($cells) {
                            $cells->setAlignment('left');
                            $cells->setFontWeight('bold');
                        });
                        // 备注 右对齐
                        $sheet->cells('D'.($tot-11), function($cells) {
                            $cells->setAlignment('right');
                        });
                        // 备注内容样式
                        $sheet->cells('E'.($tot-10).':F'.($tot-6), function($cells) {
                            $cells->setAlignment('left');
                            $cells->setFontColor('#a09b9b');
                            $cells->setFontFamily('Calibri');
                            $cells->setFontWeight('normal');
                            $cells->setFontSize(12);
                        });
                        // 总金额 高亮显示
                        $sheet->cells('F'.($tot-14), function($cells) {
                            $cells->setBackground('#87eabd');
                            $cells->setFontWeight('bold');
                            $cells->setFontSize(14);
                        });

                    });

                })->export('xls');

            }else{
                $totalItems = $res->pagination->total;
                $itemsPerPage = $res->pagination->per_page;
                $currentPage = $res->pagination->current_page;
                if ($http_build_query)
                {
                    $urlPattern = url('finance/listing?'.$http_build_query.'&page=') . '(:num)';
                }else{
                    $urlPattern = url('finance/listing') .'?page=(:num)';
                }
                $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);
                $paginator->setPreviousText('上一页');
                $paginator->setNextText('下一页');

                $data['listing']    = $res->results;
                $data['paginator']  = $paginator;
                $data['query']      = $query;

                return view('financeRecords.listing', $data);
            }

        } else {

            return redirect()
                ->back()
                ->withErrors($res->message)
                ->withInput();
        }

你可能感兴趣的:(php)