首页 > 解决方案 > Excel::download() 即使在返回后也返回一个空/空白页面

问题描述

这是我的环境

php : 7.4.2
laravel : 6.14.0
maatwbsite-excel : 3.1.18

这是导出文件

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;

class UserReportsExport implements FromArray
{

    use Exportable;

    protected $export_columns;

    function __construct($export_columns)
    {
        $this->export_columns = $export_columns;
    }

    public function array(): array
    {
        return [
            $this->export_columns
        ];
    }
}

这是控制器

return Excel::download(new UserReportsExport($export_columns), 'Users_Report_' . $todayDate . '.xlsx');

这是接近该问题的链接。根据建议的解决方案,我return在我的控制器中使用,但页面仍然是空白/空在我的/tmp文件夹中生成了一个.zip类似laravel-excel-***(它包含的所有都是 .xml 文件)。

不知道我在这里缺少什么。请建议。谢谢。

标签: phplaravellaravel-6maatwebsite-excelphp-7.4

解决方案


https://docs.laravel-excel.com/3.1/exports/collection.html#using-arrays查看它,您需要删除其中的括号,return [ $this->exports_columns ]使其变为return $this->exports_columns

如果它仍然不起作用,您可能需要ob_end_clean();在返回下载链接之前调用 a 。?> 在关闭或 var_dump 或 echo 某处弄乱您的程序流程和设置的标头之后,您可能会有一个尾随空格。如果它开始使用这个,检查你的代码,你有一些东西通过发送早期输出弄乱了你的标题。

ob_end_clean();
return Excel::download(new UserReportsExport($export_columns), 'Users_Report_' . $todayDate . '.xlsx');

另外,如果您还没有,请为所有内容打开错误报告。它可能会带来一些有意义的回报。如果您得到一个听起来很可疑的空白页,就像关闭了错误报告的错误 500。检查您的 php 日志。

在这种情况下,我能想到的最常见错误是:无法修改标头,标头已发送。请阅读如何在 PHP 中获得有用的错误消息?


推荐阅读