我使用 Laravel excel 库“maatwebsite/excel”
这是我的代码
\Excel::create('Users Report' . $time, function ($excel) use ($arrUsers) {
$excel->sheet('Users', function ($sheet) use ($arrUsers) {
// Set all margins
$sheet->fromArray($arrUsers, null, 'A1', true);
$sheet->setSize('A1', 25, 18);
$sheet->setSize('B1', 25, 18);
$sheet->setSize('C1', 25, 18);
$sheet->row(1, array(
'Id', 'Name', 'Username', 'Address', 'Email'
));
// Freeze first row
$sheet->freezeFirstRow();
$sheet->cell('A1:F1', function ($cell) {
});
});
})->store('xlsx')->download('xlsx');
文件下载成功,但打开时显示如下
我引用了以下网站并根据他的解决方案进行了尝试,但没有得到任何解决方案。 https://github.com/Maatwebsite/Laravel-Excel/issues/202
请帮帮我。
谢谢。
最佳答案
解决了输出缓冲区的问题
ob_end_clean();
ob_start(); //At the very top of your program (first line)
\Excel::create('Users Report' . $time, function ($excel) use ($arrUsers) {
$excel->sheet('Users', function ($sheet) use ($arrUsers) {
// Set all margins
$sheet->fromArray($arrUsers, null, 'A1', true);
$sheet->setSize('A1', 25, 18);
$sheet->setSize('B1', 25, 18);
$sheet->setSize('C1', 25, 18);
$sheet->row(1, array(
'Id', 'Name', 'Username', 'Address', 'Email'
));
// Freeze first row
$sheet->freezeFirstRow();
$sheet->cell('A1:F1', function ($cell) {
});
});
})->store('xlsx')->download('xlsx');
ob_flush();
https://stackoverflow.com/questions/29692749/