温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Laravel8怎么快速导出excel返回值

发布时间:2022-12-08 17:40:24 来源:亿速云 阅读:81 作者:iii 栏目:编程语言

这篇文章主要介绍了Laravel8怎么快速导出excel返回值的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Laravel8怎么快速导出excel返回值文章都会有所收获,下面我们一起来看看吧。

Laravel8导出excel返回值的简单想法

最近在使用 Maatwebsite\Excel 扩展进行 excel 的导出功能,具体怎么操作,这里不详细说了,通过下面代码导出:

//导出excel【$head是excel表头,$list是数据】
return Excel::download(new CustomerExport($head, $list), date('YmdHis') . '.xls');

我本着好奇打印这个返回值:

print_r(Excel::download(new CustomerExport($head, $list), date('YmdHis') . '.xls'));

结果如下:

Symfony\Component\HttpFoundation\BinaryFileResponse Object
(
    [file:protected] => Symfony\Component\HttpFoundation\File\File Object
        (
            [pathName:SplFileInfo:private] => /home/vagrant/www/admin/storage/framework/cache/laravel-excel/laravel-excel-4U89uL9YLn4vNb1QrCDelsmv4Yrk3Ff.xls
            [fileName:SplFileInfo:private] => laravel-excel-4U89uL9YLn4vNb1QrCDelsmv4Yrk3Ff.xls
        )
    [offset:protected] => 0
    [maxlen:protected] => -1
    [deleteFileAfterSend:protected] => 1
    [headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [public] => 1
                )
            [cookies:protected] => Array
                (
                )
            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [date] => Date
                    [last-modified] => Last-Modified
                    [content-disposition] => Content-Disposition
                )
            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => public
                        )
                    [date] => Array
                        (
                            [0] => Thu, 08 Dec 2022 05:57:26 GMT
                        )
                    [last-modified] => Array
                        (
                            [0] => Thu, 08 Dec 2022 07:16:21 GMT
                        )
                    [content-disposition] => Array
                        (
                            [0] => attachment; filename=20221208152026.xls
                        )
                )
            [cacheControl:protected] => Array
                (
                    [public] => 1
                )
        )
    [content:protected] => 
    [version:protected] => 1.0
    [statusCode:protected] => 200
    [statusText:protected] => OK
    [charset:protected] => 
)

很明显他是个对象。

因为我是前后端分离的,接口也是直接上面代码的,前端同学使用 a 标签跳到接口地址进行下载的,可以成功。但是打开 F12 的 network 查看返回值,前端拿到的是文件流

为什么直接运行接口返回的是个对象,前端拿到居然变为文件流了?

原来是返回的时候,自动给返回头加了两个参数

Content-Disposition:attachment; filename=20221208152026.xls
Content-Type:application/vnd.ms-excel

Content-Disposition 不就是对象里面的头消息嘛。

关于“Laravel8怎么快速导出excel返回值”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Laravel8怎么快速导出excel返回值”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI