laravel安装excel功能

原文安装链接:https://github.com/Maatwebsite/Laravel-Excel

代码如下:

  1        if ($rows = DB::connection('glist')->table('program_view')->where('v_type', $type)->get())
  2             {
  3                 $head          = array('Pid', 'Name', 'Play Times');
  4                 $height        = array();
  5                 $movie_prog    = array();
  6                 $movie_height  = array();
  7                 $series_prog   = array();
  8                 $series_height = array();
  9     
 10                 foreach ($rows as $row)
 11                 {
 12                     $pro_pid   = $row->v_pid;
 13                     $pro_name  = $row->v_prog;
 14                     $play_time = 0;
 15                         
 16                     if ($counts = DB::connection('glist')->table('program_date_view')->where('prog_id', $pro_pid)
 17                             ->whereBetween('date', array($start_time, $end_time))->lists('view_count'))
 18                     {
 19                         foreach ($counts as $count)
 20                         {
 21                             $play_time += $count;
 22                         }
 23                     }
 24                         
 25                     $play_times[] = strval($play_time);
 26                     $prog[]       = array($pro_pid, $pro_name, strval($play_time));
 27                         
 28                     if (isset($this->mMonitorProgTypes[$pro_pid]) &&
 29                         self::VOD_PROGRAM_TYPE == $this->mMonitorProgTypes[$pro_pid] &&
 30                         isset($this->mMonitorProgCategories[$pro_pid]) &&
 31                         "Series" != $this->mMonitorProgCategories[$pro_pid])
 32                     {
 33                         $movie_play_times[] = strval($play_time);
 34                         $movie_prog[]         = array($pro_pid, $pro_name, strval($play_time));
 35                     }
 36                     else if (isset($this->mMonitorProgTypes[$pro_pid]) &&
 37                             self::VOD_PROGRAM_TYPE == $this->mMonitorProgTypes[$pro_pid] &&
 38                             isset($this->mMonitorProgCategories[$pro_pid]) &&
 39                             "Series" == $this->mMonitorProgCategories[$pro_pid])
 40                     {
 41                         $series_play_times[] = strval($play_time);
 42                         $series_prog[]          = array($pro_pid, $pro_name, strval($play_time));
 43                     }
 44                 }
 45     
 46                 if (!empty($prog))
 47                 {
 48                     array_multisort($play_times, SORT_DESC, $prog);
 49                     array_unshift($prog, $head);
 50                     $height = array_fill(0, count($prog) + 1, 20);               //20——excel cell height
 51                 }
 52     
 53                 if (!empty($movie_prog))
 54                 {
 55                     array_multisort($movie_play_times, SORT_DESC, $movie_prog);
 56                     array_unshift($movie_prog, $head);
 57                     $movie_height = array_fill(0, count($movie_prog) + 1, 20);
 58                 }
 59     
 60                 if (!empty($series_prog))
 61                 {
 62                     array_multisort($series_play_times, SORT_DESC, $series_prog);
 63                     array_unshift($series_prog, $head);
 64                     $series_height = array_fill(0, count($series_prog) + 1, 20);
 65                 }
 66             }
 67                 
 68             Excel::create($type . ' Program Statistics Data', function($excel) use ($type, $prog, $height, $movie_prog, $movie_height, $series_prog, $series_height) {
 69                     
 70                 $excel->sheet('program_ranking', function($sheet) use ($prog, $height) {
 71                         
 72                     $sheet->fromArray($prog, null, 'A1', false, false);
 73                     $sheet->setWidth(array('A' =>  25, 'B' =>  35, 'C' => 15));
 74                     $sheet->setHeight($height);
 75                         
 76                     $sheet->cells('A1:C1', function ($cells) {
 77                         $cells->setFont(array(
 78                                 'family'     => 'Calibri',
 79                                 'size'       => '14',
 80                                 'bold'       =>  true
 81                         ));
 82                     });
 83                             
 84                     $sheet->cells('A1:C' . count($height), function ($cells) {
 85                         $cells->setAlignment('center');
 86                         $cells->setValignment('middle');
 87                     });
 88                 });
 89     
 90                 if ("VOD" == $type)
 91                 {
 92                     $excel->sheet('series_ranking', function($sheet) use ($series_prog, $series_height) {
 93                                     
 94                         $sheet->fromArray($series_prog, null, 'A1', false, false);
 95                         $sheet->setWidth(array('A' =>  25, 'B' =>  35, 'C' => 15));
 96                         $sheet->setHeight($series_height);
 97                                     
 98                         $sheet->cells('A1:C1', function ($cells) {
 99                                 $cells->setFont(array(
100                                         'family'     => 'Calibri',
101                                         'size'       => '14',
102                                         'bold'       =>  true
103                                 ));
104                         });
105                                         
106                         $sheet->cells('A1:C' . count($series_height), function ($cells) {
107                                 $cells->setAlignment('center');
108                                 $cells->setValignment('middle');
109                         });
110                     });
111                                 
112                     $excel->sheet('other_ranking', function($sheet) use ($movie_prog, $movie_height) {
113                                     
114                         $sheet->fromArray($movie_prog, null, 'A1', false, false);
115                         $sheet->setWidth(array('A' =>  25, 'B' =>  35, 'C' => 15));
116                         $sheet->setHeight($movie_height);
117                                     
118                         $sheet->cells('A1:C1', function ($cells) {
119                                 $cells->setFont(array(
120                                         'family'     => 'Calibri',
121                                         'size'       => '14',
122                                         'bold'       =>  true
123                                 ));
124                         });
125     
126                         $sheet->cells('A1:C' . count($movie_height), function ($cells) {
127                                 $cells->setAlignment('center');
128                                 $cells->setValignment('middle');
129                         });
130                     });
131                 }
132             })->export('xls');
133         }
原文地址:https://www.cnblogs.com/linguoguo/p/4172569.html