Laravel Excel3.0如何導出

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由laravel教程欄目給大家介紹Laravel Excel3.0導出方法,希望對需要的朋友有所幫助!

導出方法抽離:

<?php namespace AppExports; use MaatwebsiteExcelConcernsFromCollection; use MaatwebsiteExcelConcernsShouldAutoSize; use MaatwebsiteExcelConcernsWithColumnFormatting; use MaatwebsiteExcelConcernsWithCustomValueBinder; use MaatwebsiteExcelConcernsWithEvents; use MaatwebsiteExcelConcernsWithStrictNullComparison; use MaatwebsiteExcelEventsAfterSheet; use PhpOfficePhpSpreadsheetCellStringValueBinder; use PhpOfficePhpSpreadsheetStyleNumberFormat;  class Export extends StringValueBinder implements FromCollection,     ShouldAutoSize,WithColumnFormatting,WithCustomValueBinder,WithStrictNullComparison,WithEvents {     private $row;     private $data;     private $mergeCell;     private $columnName;     private $formatNumber;      /*      * $mergeCell $columnName :合并單元格所需參數;      * $mergeCell 需要合并的位置數組以MAP形式存儲 [開始行=>結束行] ?????*?$columnName?需要合并列?與合并行數結合使用ARRAY存儲?['A','B'] ?????*/ ????public?function?__construct($row,$data,$mergeCell=null,$columnName=null,$formatNumber=[]) ????{ ????????$this-&gt;row?=?$row; ????????$this-&gt;data?=?$data; ????????$this-&gt;mergeCell?=?$mergeCell; ????????$this-&gt;columnName?=?$columnName; ????????$this-&gt;formatNumber?=?$formatNumber; ????}  ????public?function?collection() ????{ ????????$row?=?$this-&gt;row; ????????$data?=?$this-&gt;data;  //設置表頭 ????????foreach?($row[0]?as?$key?=&gt;?$value)?{ ????????????$key_arr[]?=?$key; ????????}  //輸入數據 ????????foreach?($data?as?$key?=&gt;?&amp;$value)?{ ????????????$js?=?[]; ????????????for?($i=0;?$i??$value[?$key_arr[$i]?]?]); ????????????} ????????????array_push($row,?$js); ????????????unset($val); ????????} ????????return?collect($row); ????} ????public?function?registerEvents():?array ????{ ????????//?TODO:?Implement?registerEvents()?method. ????????if?($this-&gt;mergeCell?&amp;&amp;?$this-&gt;columnName){ ????????????return?[ ????????????????AfterSheet::class?=&gt;?function(AfterSheet?$event){ ????????????????????foreach?($this-&gt;columnName?as?$column){ ????????????????????????foreach?($this-&gt;mergeCell?as?$key=&gt;$value){ ????????????????????????????$event-&gt;sheet-&gt;getDelegate()-&gt;mergeCells($column.$key.':'.$column.$value); ????????????????????????} ????????????????????} ????????????????} ????????????]; ????????} ????????return?[]; ????}  ????public?function?columnFormats():?array{ ????????$formatNumber?=?[]; ????????foreach?($this-&gt;formatNumber?as?$column){ ????????????$formatNumber[$column]?=?NumberFormat::FORMAT_TEXT; ????????} ????????return?$formatNumber; ????} }

使用:

/*表頭表體都為二維數組*/ $row=[['row1'=&gt;'列1','row2'=&gt;'列2']]; /*與表頭key對應,缺少數據報錯*/ $list=[['row1'=&gt;'行1列1','row2'=&gt;'行1列2'],['row1'=&gt;'行2列1','row2'=&gt;'行2列2']]; /*將第一行到第三行,第五行到第七行的A,B,C列各自合并*/ $mergeCell=[1=&gt;3,5=&gt;7]; $columnName=["A","B","C"]; /*數字過長的列轉換格式防止科學計數*/ $formatNumber=['A','B','C']; //上方A,B,C列都為示意,根據自己需求調整,對應EXCEL的列 return?Excel::download(new?Export($row,$list,$mergeCell,$columnName,$formatNumber),'fileName');

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享