Laravel8如何快速导出excel返回值!

本篇文章主要给大家介绍怎么快速实现laravel8导出excel返回值,很简单哦~希望对需要的朋友有所帮助!

【相关推荐:laravel视频教程】

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

最近在使用 MaatwebsiteExcel 扩展进行 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'));

结果如下:

SymfonyComponentHttpFoundationBinaryFileResponse Object(    [file:protected] => SymfonyComponentHttpFoundationFileFile 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] => SymfonyComponentHttpFoundationResponseHeaderBag 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 查看返回值,前端拿到的是文件流,如下:

9a35facc888bd226277cb136b859350.jpg

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

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

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

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

4089bac883b56136b7087940f971b70.jpg

以上就是Laravel8如何快速导出excel返回值!的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/67253.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月12日 10:45:27
下一篇 2025年11月12日 11:38:27

相关推荐

发表回复

登录后才能评论
关注微信