0

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Brendan White
  • 435
  • 2
  • 8
  • 17
  • have you echo the data? – kunal Dec 18 '17 at 11:55
  • Yes. Normally, the data for People is fine but the data for Finance is broken - see https://jsfiddle.net/qurysp9r/ If I set `excel.import.startRow` to 3, then People is now broken because I skipped the first two rows including the header row - see https://jsfiddle.net/s1szqjnj/ – Brendan White Dec 18 '17 at 21:13

2 Answers2

5

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;
}
0

So I've come up with an answer. It's not a great solution, but I guess it will do.

I will just import the whole document, and process the People tab and ignore the Finance tab.

Then I will set excel.import.startRow to 3 and import the whole document a second time, and this time I'll ignore the People tab and process the Finance tab.

Like I say this is not an elegant solution so if anyone comes up with a better way to do it I'd love to hear from you. But for now it will do.

Brendan White
  • 435
  • 2
  • 8
  • 17