Home > Net >  Read especific sheet by name on import excel laravel
Read especific sheet by name on import excel laravel

Time:10-28

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

My problem it's that i have multiples sheets, and dosn't the same order or there is not the same number of pages. Then, i must identify the sheet by name on my import class (laravel import).

Here 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 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
    }
}

CodePudding user response:

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

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