我使用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
请帮帮我。
感谢。
答案 0 :(得分:7)
输出缓冲区中的问题由
解决 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();
答案 1 :(得分:0)
当您的代码向屏幕发送任何警告或错误时,也会发生这种情况,请尝试output buffering,以查看您的程序是否将任何字符发送到输出:
<?php
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');
//Catch any character sended to the output at this point
$out = ob_get_contents();
//Routing the output to the error_log
error_log($out);
//Cleaning the ouput buffer
ob_end_clean();
我希望这对你有用