本文主要是介绍Maatwebsite / Laravel-Excel 3.1(只适用3.1以上),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.安装
composer require maatwebsite/excel
该Maatwebsite\Excel\ExcelServiceProvider是自动发现,并在默认情况下注册,但如果你想自己注册它:
添加 ServiceProvider config/app.php
'providers' => [/** Package Service Providers...*/Maatwebsite\Excel\ExcelServiceProvider::class,
]
添加 Facade in config/app.php
'aliases' => [...'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
导出类代码(app\Tools\ExcelExports.php)
<?php
namespace App\Tools;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings; //设置标题
use Maatwebsite\Excel\Concerns\WithEvents; //格式化单元格
use Maatwebsite\Excel\Events\AfterSheet; //格式化单元格/*** Excel导出通用类*/
class ExcelExports implements FromCollection, WithHeadings ,WithEvents
{use Exportable;private $data;private $headings;//数据注入public function __construct($data, $headings){$this->data = $data;$this->headings = $headings;}//实现FromCollection接口public function collection(){return collect($this->data);}//实现WithHeadings接口public function headings(): array{return $this->headings;}/*** @return array*/public function registerEvents(): array{return [AfterSheet::class => function(AfterSheet $event) {// ... 此处你可以任意格式化$event->sheet->getDelegate()->getColumnDimension('A')->setWidth(30); //设置宽度},];}
}
导出类代码(app\Tools\ExcelImport.php)
<?phpnamespace App\Tools;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
/*** Excel导入通用类*/
class ExcelImport implements ToCollection
{public $data;protected $delTitle;/**** @param $title integer //去掉几行标题 默认一行*/public function __construct($delTitle = 1){$this->delTitle = $delTitle;}/*** @param Collection $rows*/public function collection(Collection $rows){$this->delTitle($rows);//$rows 是数组格式$this->data = $rows;foreach ($rows as $row) {//这就是你导入的内容var_dump($row);die;}}public function delTitle (&$rows) {$rows = $rows->slice($this->delTitle)->values();}
}
调用方法(php artisan make:controller ExcelController)
<?phpnamespace App\Http\Controllers;
use App\Tools\ExcelExports;
use App\Tools\ExcelImport;
use Illuminate\Http\Request;
use Excel;
use DB;class ExcelController extends Controller
{public function export(){$cellData = DB::table('user')->select('id','name')->get()->toArray();$header = ['id' => '编号', 'name' => '名字', ];return Excel::download(new ExcelExports($cellData,$header), 'users.xlsx');}//Excel文件导入功能public function import(){$filePath = iconv('UTF-8', 'GBK', '20201230090705').'.xls';// 'imports' 在\config\filesystems.php 添加以下代码// 'disks' => [// ... // 'imports' => [// 'driver' => 'local',// 'root' => public_path('uploads/behaviors/'), //导入文件存放的目录// 'url' => env('APP_URL').'/public',// 'visibility' => 'public',// ],// ],Excel::import(new ExcelImport, $filePath, 'imports');}}
根据以下文章所写
使用方法:
http://www.edbiji.com/doccenter/showdoc/209/nav/3722.html
https://www.cnblogs.com/niuben/p/11458450.html
设置列宽等其他功能使用方法:
https://learnku.com/laravel/t/24161
https://blog.csdn.net/lihongxian/article/details/106521367?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control
这篇关于Maatwebsite / Laravel-Excel 3.1(只适用3.1以上)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!