本篇文章主要給大家介紹怎么快速實(shí)現(xiàn)laravel8導(dǎo)出excel返回值,很簡單哦~希望對(duì)需要的朋友有所幫助!
【相關(guān)推薦:laravel視頻教程】
Laravel8導(dǎo)出excel返回值的簡單想法
最近在使用 MaatwebsiteExcel 擴(kuò)展進(jìn)行 excel 的導(dǎo)出功能,具體怎么操作,這里不詳細(xì)說了,通過下面代碼導(dǎo)出:
//導(dǎo)出excel【$head是excel表頭,$list是數(shù)據(jù)】 return?Excel::download(new?CustomerExport($head,?$list),?date('YmdHis')?.?'.xls');
我本著好奇打印這個(gè)返回值:
print_r(Excel::download(new?CustomerExport($head,?$list),?date('YmdHis')?.?'.xls'));
結(jié)果如下:
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]?=>? )
很明顯他是個(gè)對(duì)象。
因?yàn)槲沂乔昂蠖朔蛛x的,接口也是直接上面代碼的,前端同學(xué)使用 a 標(biāo)簽跳到接口地址進(jìn)行下載的,可以成功。但是打開 F12 的 network 查看返回值,前端拿到的是文件流,如下:
為什么直接運(yùn)行接口返回的是個(gè)對(duì)象,前端拿到居然變?yōu)槲募髁耍?/p>
原來是返回的時(shí)候,自動(dòng)給返回頭加了兩個(gè)參數(shù)
Content-Disposition:attachment;?filename=20221208152026.xls Content-Type:application/vnd.ms-excel
Content-Disposition 不就是對(duì)象里面的頭消息嘛
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END