您好,登錄后才能下訂單哦!
這篇文章主要介紹了Laravel Excel3.0導(dǎo)出的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
導(dǎo)出方法抽離:
<?php namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\ShouldAutoSize; use Maatwebsite\Excel\Concerns\WithColumnFormatting; use Maatwebsite\Excel\Concerns\WithCustomValueBinder; use Maatwebsite\Excel\Concerns\WithEvents; use Maatwebsite\Excel\Concerns\WithStrictNullComparison; use Maatwebsite\Excel\Events\AfterSheet; use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; class Export extends StringValueBinder implements FromCollection, ShouldAutoSize,WithColumnFormatting,WithCustomValueBinder,WithStrictNullComparison,WithEvents { private $row; private $data; private $mergeCell; private $columnName; private $formatNumber; /* * $mergeCell $columnName :合并單元格所需參數(shù); * $mergeCell 需要合并的位置數(shù)組以MAP形式存儲(chǔ) [開(kāi)始行=>結(jié)束行] * $columnName 需要合并列 與合并行數(shù)結(jié)合使用ARRAY存儲(chǔ) ['A','B'] */ public function __construct($row,$data,$mergeCell=null,$columnName=null,$formatNumber=[]) { $this->row = $row; $this->data = $data; $this->mergeCell = $mergeCell; $this->columnName = $columnName; $this->formatNumber = $formatNumber; } public function collection() { $row = $this->row; $data = $this->data; //設(shè)置表頭 foreach ($row[0] as $key => $value) { $key_arr[] = $key; } //輸入數(shù)據(jù) foreach ($data as $key => &$value) { $js = []; for ($i=0; $i < count($key_arr); $i++) { $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]); } array_push($row, $js); unset($val); } return collect($row); } public function registerEvents(): array { // TODO: Implement registerEvents() method. if ($this->mergeCell && $this->columnName){ return [ AfterSheet::class => function(AfterSheet $event){ foreach ($this->columnName as $column){ foreach ($this->mergeCell as $key=>$value){ $event->sheet->getDelegate()->mergeCells($column.$key.':'.$column.$value); } } } ]; } return []; } public function columnFormats(): array{ $formatNumber = []; foreach ($this->formatNumber as $column){ $formatNumber[$column] = NumberFormat::FORMAT_TEXT; } return $formatNumber; } }
使用:
/*表頭表體都為二維數(shù)組*/ $row=[['row1'=>'列1','row2'=>'列2']]; /*與表頭key對(duì)應(yīng),缺少數(shù)據(jù)報(bào)錯(cuò)*/ $list=[['row1'=>'行1列1','row2'=>'行1列2'],['row1'=>'行2列1','row2'=>'行2列2']]; /*將第一行到第三行,第五行到第七行的A,B,C列各自合并*/ $mergeCell=[1=>3,5=>7]; $columnName=["A","B","C"]; /*數(shù)字過(guò)長(zhǎng)的列轉(zhuǎn)換格式防止科學(xué)計(jì)數(shù)*/ $formatNumber=['A','B','C']; //上方A,B,C列都為示意,根據(jù)自己需求調(diào)整,對(duì)應(yīng)EXCEL的列 return Excel::download(new Export($row,$list,$mergeCell,$columnName,$formatNumber),'fileName');
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享Laravel Excel3.0導(dǎo)出的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問(wèn)題就找億速云,詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。