版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cofecode/article/details/84325095
安装 maatwebsite/excel
blade模板
<p>
<button onclick="scoreExcel()">导出列表</button>
</p>
<table border="1" cellspacing="0">
<tr>
<th>姓名</th>
<th>分数</th>
<th>科目</th>
<th>时间</th>
</tr>
@foreach($lists as $value)
<tr>
<td>{{$value -> name}}</td>
<td>{{$value -> score}}</td>
<td>{{$value -> subject}}</td>
<td>{{$value -> updated_at}}</td>
</tr>
@endforeach
</table>
<script>
function scoreExcel() {
window.location.href = 'scoreExcel'
}
</script>
控制器
// 导出
public function scoreExcel() {
DB::setFetchMode(PDO::FETCH_ASSOC);
$arr = DB::table('test')->get();
$data = [
[
'姓名',
'分数',
'科目',
'时间'
]
];
for ($i=1; $i < count($arr); $i++) {
$data[$i] = [
$arr[$i]['name'],
$arr[$i]['score'],
$arr[$i]['subject'],
$arr[$i]['updated_at'],
];
}
Excel::create(iconv('UTF-8', 'GBK', '成绩单'), function ($excel) use ($data) {
$excel->sheet('recored', function ($sheet) use ($data) {
$sheet->rows($data);
});
})->export('xls');
}
$arr 数据结构