Read specific sheet by name on import excel laravel

I have an excel with multiple sheets but i only want read one sheet: enter image description here

My problem is that i have multiple sheets, and they do not have the same order or the same number of pages. Then, i must identify the sheet by name on my import class (laravel import).

Here is my import class:

use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;

class ExecutedUPS implements WithMultipleSheets, WithEvents
{
    private $nameSheetUPS = "LMS - F";

    public function sheets(): array
    {
        $sheets = [];

        //doesn't work for me
        if($event->getSheet()->getTitle() === $this->nameSheetUPS){
            $sheets[] = new SheetUPS();
        }
        
        return $sheets;
    }
}

And here is my class "SheetUPS":

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class SheetUPS implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
        //this i know do
    }
}

Upvotes: 1

Views: 2627

Answers (1)

Professor
Professor

Reputation: 908

Have you tried selecting the sheet by name as the documentation does?

//class ExecutedUPS
public function sheets(): array
{
    return [
        'LMS - F' => new SheetUPS(),
    ];
}

Upvotes: 1

Related Questions