Search code examples
laravelmaatwebsite-excelphpdotenv

Can I set startRow to different values for different sheets using Maatwebsite's Laravel-Excel?


I'm using Maatwebsite's Laravel-Excel to import an Excel file with two sheets. This file is generated by another system; I can't change the way it is produced.

On the first sheet, the first row is the column headings. This works fine.

First sheet has headers in first row

...but the second sheet has two rows of unwanted text, and then the headers in the third row.

Second sheet has headers in third row

I can set config(['excel.import.startRow' => 3]) to start importing at the third row, but that means I miss the first two rows of useful data in the first sheet.

    config(['excel.import.startRow' => 3]);
    $sheets = $import->all();

Is there any way I can leave excel.import.startRow set to 1 for the first sheet, but set excel.import.startRow to 3 for the other sheet?


Solution

  • You can use:

    use Maatwebsite\Excel\Concerns\WithHeadingRow;
    use Maatwebsite\Excel\Concerns\WithStartRow;
    

    In class:

    class nameClass implements ToModel, WithHeadingRow, WithStartRow
    

    add the functions:

     public function headingRow(): int
    {
        return 3;
    }
    
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 4;
    }