3月 20, 2023 - laravel    Laravel6 export customize data to excel file.已关闭评论

Laravel6 export customize data to excel file.

使用composer包工具:maatwebsite/excel

"maatwebsite/excel": "^3.1

步骤:

1:先定义一个export的Object

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::default('none');

class UsersImport implements FromArray
{

    protected $invoices;

    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function array(): array
    {
        return $this->invoices;
    }
}

2:向Excel文件写内容:

Excel::store(new UsersImport([
    [1, 23, 433],
    [22, 2332, 43333],
    [223, 44, 53],
]), 'public/test.xlsx', 'local');

3:效果:

参考链接:https://docs.laravel-excel.com/3.1/exports/collection.html

评论被关闭。