当前位置:网站首页>Laravel8 export excel file

Laravel8 export excel file

2022-07-05 03:58:00 s_ Canned ice cubes

1. Reference resources Excle Official website .

Laravel | Medium Superged Excel Export and import Laravel Excel (laravel-excel.com)icon-default.png?t=M5H6https://laravel-excel.com/

2. Use composer Command to deploy Excle.

composer require maatwebsite/excel

3. Use composer Command to create Excle The export model .

php artisan make:export UsersExport --model=User

4. Click to enter the newly created Excle controller . 

 5. The contents of the document are modified

  notes : If we export excle Header words , We need to inherit our header file .

 

 

  Just write our exported method in our control :

  Finally, call our method through the route :

Attach complete code :

Excle Content of model file :

<?php

namespace App\Exports;

use App\Models\AddlistModel;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;// export excle Header 

class UsersExport implements FromCollection, WithHeadings
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        // Here we call the model we need to export data 
        return AddlistModel::all();
    }

    // Add the specified header 
    public function headings(): array
    {
        return [
            'ID',
            ' title ',
            ' Text ',
            ' Creation time ',
            ' Modification time '
        ];
    }


}

  Controller code content :

// Export us Excl file 
    public function export()
    {
        return Excel::download(new UsersExport, 'excle.xlsx');
    }

原网站

版权声明
本文为[s_ Canned ice cubes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050343567886.html