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

 3272

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


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

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

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

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

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

结果如下:

  1. Symfony\Component\HttpFoundation\BinaryFileResponse Object
  2. (
  3.     [file:protected] => Symfony\Component\HttpFoundation\File\File Object
  4.         (
  5.             [pathName:SplFileInfo:private] => /home/vagrant/www/admin/storage/framework/cache/laravel-excel/laravel-excel-4U89uL9YLn4vNb1QrCDelsmv4Yrk3Ff.xls
  6.             [fileName:SplFileInfo:private] => laravel-excel-4U89uL9YLn4vNb1QrCDelsmv4Yrk3Ff.xls
  7.         )
  8.     [offset:protected] => 0
  9.     [maxlen:protected] => -1
  10.     [deleteFileAfterSend:protected] => 1
  11.     [headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
  12.         (
  13.             [computedCacheControl:protected] => Array
  14.                 (
  15.                     [public] => 1
  16.                 )
  17.             [cookies:protected] => Array
  18.                 (
  19.                 )
  20.             [headerNames:protected] => Array
  21.                 (
  22.                     [cache-control] => Cache-Control
  23.                     [date] => Date
  24.                     [last-modified] => Last-Modified
  25.                     [content-disposition] => Content-Disposition
  26.                 )
  27.             [headers:protected] => Array
  28.                 (
  29.                     [cache-control] => Array
  30.                         (
  31.                             [0] => public
  32.                         )
  33.                     [date] => Array
  34.                         (
  35.                             [0] => Thu, 08 Dec 2022 05:57:26 GMT
  36.                         )
  37.                     [last-modified] => Array
  38.                         (
  39.                             [0] => Thu, 08 Dec 2022 07:16:21 GMT
  40.                         )
  41.                     [content-disposition] => Array
  42.                         (
  43.                             [0] => attachment; filename=20221208152026.xls
  44.                         )
  45.                 )
  46.             [cacheControl:protected] => Array
  47.                 (
  48.                     [public] => 1
  49.                 )
  50.         )
  51.     [content:protected] => 
  52.     [version:protected] => 1.0
  53.     [statusCode:protected] => 200
  54.     [statusText:protected] => OK
  55.     [charset:protected] => 
  56. )

很明显他是个对象。

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


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


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

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

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

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


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

本文网址:https://www.zztuku.com/index.php/detail-13531.html
站长图库 - Laravel8如何快速导出excel返回值!
申明:本文转载于《learnku》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐