Laravel-excel: [3.1] How to select a specific sheet to import?

Created on 11 Jun 2019  路  9Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.2.19-0ubuntu0.18.10.1
  • Laravel version: 5.5
  • Package version: 3.1

Description

Hi, I have a excel file with 9 sheet's. Have a way to specify a sheet to import? I need only the first sheet, I don't found in documentation (except 2.1) a way to specify this.

Edit: I found this:

        $import->onlySheets('worksheet 1');

But I'm not have the name of the sheet I need to import. Have a way to specify the '0'|'1' sheet?

Most helpful comment

I found here: https://github.com/Maatwebsite/Laravel-Excel/issues/2017

class EventsImport implements ToCollection, WithMultipleSheets
{
.....
    /**
     * @return array
     */
    public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }
}

All 9 comments

Have you tried Selecting sheets by worksheet index?
I think onlySheets only works with sheet names and not with index.

I can make the reference to same class to not make a new class?

I can make the reference to same class to not make a new class?

You could try, but not sure if that would actually work. To be safe, I'd use two separate classes.

From the docs:

When a file has multiple sheets, each sheet will go through the import object. If you want to handle each sheet separately, you'll need to implement the WithMultipleSheets concern.

I found here: https://github.com/Maatwebsite/Laravel-Excel/issues/2017

class EventsImport implements ToCollection, WithMultipleSheets
{
.....
    /**
     * @return array
     */
    public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }
}

But, now cells with formula has comming with formula value, not the 'value' of the result. Have any idea?

Edit: The time to load the sheet is the same, using or not only first sheet inside.

See our list of Import concerns. There's a WithCalculatedFormulas concern.

See our list of Import concerns. There's a WithCalculatedFormulas concern.

Works, where's the result for reference:

class EventsImport implements ToCollection , WithMultipleSheets, WithCalculatedFormulas
{
.....
    /**
     * @return array
     */
    public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }

    /**
     * @param Collection $collection
     * @throws \Exception
     */
    public function collection(Collection $collection)
    {
        ......
    }
}

Glad to hear your issue has been resolved. Thank you for using Laravel Excel!

@kidjapa worked perfectly. but what if i just want to get data from 2nd sheet

Was this page helpful?
0 / 5 - 0 ratings

Related issues

disto picture disto  路  3Comments

alejandri picture alejandri  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

bahmanyaghoobi picture bahmanyaghoobi  路  3Comments

muhghazaliakbar picture muhghazaliakbar  路  3Comments