本篇文章主要給大家介紹怎么快速實現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 查看返回值,前端拿到的是文件流,如下:
為什么直接運行接口返回的是個對象,前端拿到居然變為文件流了?
原來是返回的時候,自動給返回頭加了兩個參數
Content-Disposition:attachment;?filename=20221208152026.xls Content-Type:application/vnd.ms-excel
Content-Disposition 不就是對象里面的頭消息嘛
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END