laravel安装excel功能

时间:2021-09-07 15:40:49

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

代码如下:

        if ($rows = DB::connection('glist')->table('program_view')->where('v_type', $type)->get())
{
$head = array('Pid', 'Name', 'Play Times');
$height = array();
$movie_prog = array();
$movie_height = array();
$series_prog = array();
$series_height = array(); foreach ($rows as $row)
{
$pro_pid = $row->v_pid;
$pro_name = $row->v_prog;
$play_time = 0; if ($counts = DB::connection('glist')->table('program_date_view')->where('prog_id', $pro_pid)
->whereBetween('date', array($start_time, $end_time))->lists('view_count'))
{
foreach ($counts as $count)
{
$play_time += $count;
}
} $play_times[] = strval($play_time);
$prog[] = array($pro_pid, $pro_name, strval($play_time)); if (isset($this->mMonitorProgTypes[$pro_pid]) &&
self::VOD_PROGRAM_TYPE == $this->mMonitorProgTypes[$pro_pid] &&
isset($this->mMonitorProgCategories[$pro_pid]) &&
"Series" != $this->mMonitorProgCategories[$pro_pid])
{
$movie_play_times[] = strval($play_time);
$movie_prog[] = array($pro_pid, $pro_name, strval($play_time));
}
else if (isset($this->mMonitorProgTypes[$pro_pid]) &&
self::VOD_PROGRAM_TYPE == $this->mMonitorProgTypes[$pro_pid] &&
isset($this->mMonitorProgCategories[$pro_pid]) &&
"Series" == $this->mMonitorProgCategories[$pro_pid])
{
$series_play_times[] = strval($play_time);
$series_prog[] = array($pro_pid, $pro_name, strval($play_time));
}
} if (!empty($prog))
{
array_multisort($play_times, SORT_DESC, $prog);
array_unshift($prog, $head);
$height = array_fill(0, count($prog) + 1, 20); //20——excel cell height
} if (!empty($movie_prog))
{
array_multisort($movie_play_times, SORT_DESC, $movie_prog);
array_unshift($movie_prog, $head);
$movie_height = array_fill(0, count($movie_prog) + 1, 20);
} if (!empty($series_prog))
{
array_multisort($series_play_times, SORT_DESC, $series_prog);
array_unshift($series_prog, $head);
$series_height = array_fill(0, count($series_prog) + 1, 20);
}
} Excel::create($type . ' Program Statistics Data', function($excel) use ($type, $prog, $height, $movie_prog, $movie_height, $series_prog, $series_height) { $excel->sheet('program_ranking', function($sheet) use ($prog, $height) { $sheet->fromArray($prog, null, 'A1', false, false);
$sheet->setWidth(array('A' => 25, 'B' => 35, 'C' => 15));
$sheet->setHeight($height); $sheet->cells('A1:C1', function ($cells) {
$cells->setFont(array(
'family' => 'Calibri',
'size' => '14',
'bold' => true
));
}); $sheet->cells('A1:C' . count($height), function ($cells) {
$cells->setAlignment('center');
$cells->setValignment('middle');
});
}); if ("VOD" == $type)
{
$excel->sheet('series_ranking', function($sheet) use ($series_prog, $series_height) { $sheet->fromArray($series_prog, null, 'A1', false, false);
$sheet->setWidth(array('A' => 25, 'B' => 35, 'C' => 15));
$sheet->setHeight($series_height); $sheet->cells('A1:C1', function ($cells) {
$cells->setFont(array(
'family' => 'Calibri',
'size' => '14',
'bold' => true
));
}); $sheet->cells('A1:C' . count($series_height), function ($cells) {
$cells->setAlignment('center');
$cells->setValignment('middle');
});
}); $excel->sheet('other_ranking', function($sheet) use ($movie_prog, $movie_height) { $sheet->fromArray($movie_prog, null, 'A1', false, false);
$sheet->setWidth(array('A' => 25, 'B' => 35, 'C' => 15));
$sheet->setHeight($movie_height); $sheet->cells('A1:C1', function ($cells) {
$cells->setFont(array(
'family' => 'Calibri',
'size' => '14',
'bold' => true
));
}); $sheet->cells('A1:C' . count($movie_height), function ($cells) {
$cells->setAlignment('center');
$cells->setValignment('middle');
});
});
}
})->export('xls');
}