日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js 导出excel_Laravel Excel3.0导出

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js 导出excel_Laravel Excel3.0导出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php中文網最新課程

每日17點準時技術干貨分享

導出方法抽離:

<?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 :合并單元格所需參數; * $mergeCell 需要合并的位置數組以MAP形式存儲 [開始行=>結束行] * $columnName 需要合并列 與合并行數結合使用ARRAY存儲 ['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;//設置表頭 foreach ($row[0] as $key => $value) { $key_arr[] = $key; }//輸入數據 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; }}

使用:

/*表頭表體都為二維數組*/$row=[['row1'=>'列1','row2'=>'列2']];/*與表頭key對應,缺少數據報錯*/$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"];/*數字過長的列轉換格式防止科學計數*/$formatNumber=['A','B','C'];//上方A,B,C列都為示意,根據自己需求調整,對應EXCEL的列return Excel::download(new Export($row,$list,$mergeCell,$columnName,$formatNumber),'fileName');

總結

以上是生活随笔為你收集整理的js 导出excel_Laravel Excel3.0导出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。