[Vuejs]-Laravel Excel 3.0/3.1 export foreach

0👍

All you need to do is :

Create one class ExportReport.php

<?php

namespace NeonMobile\Exports;


class ExportReport implements WithMultipleSheets
{
    use Exportable; 

    public function __construct($data = [])
    {
        $this->myRepo = \App::make(ReportRepositoryInterface::class);
    }

    /**
     * @return array
     */
    public function sheets(): array
    {
        $reportData= $this->myRepo->myFunctionToGetData();
        $sheets[] = new ExportMultipleSheets([
            'collection' => collect($reportData),
            'title'      => 'export_invoice',
            'cells'      => ['A1:G1'],
        ]);

        return $sheets;
    }
}

And Another common class ExportMultipleSheets.php

<?php

namespace NeonMobile\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;

class ExportMultipleSheets implements FromCollection, WithTitle, ShouldAutoSize, WithEvents
{
    private $title;
    private $collection;
    private $cells;

    public function __construct($data = [])
    {
        $this->title = $data['title'];
        $this->collection = $data['collection'];
        $this->cells = $data['cells'];
    }

    /**
     * @return Collection
     */
    public function collection()
    {
        return $this->collection;
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return $this->title;
    }
}

Now call just

class ReportController extends Controller{
    return (new ReportExport)->download('report.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}

Leave a comment