Laravel-excel: [QUESTION] Excel Import select sheet1 or specific sheet

Created on 27 Jan 2019  路  4Comments  路  Source: Maatwebsite/Laravel-Excel

  • PHP version: 7.2.13
  • Laravel version: 5.7.22
  • Package version: 3.1.3

Description

My excel file have 3 sheets (Standard excel come with 3 sheets). How to I set I only want to import the 1st sheet?

I am using ToCollection(), after imported 1 sheet, I will need to return array. I notice the default collection() is @return by null.

Below is my dump output, I put a dump before and after import. I notice inside StockImport.php run 3 times. How do I specific import 1 sheet?

"Start Import"

 ------------ ---------------------------------
  date         Sun, 27 Jan 2019 02:37:21 +0000
  controller   "StockImportController"
  source       StockImport.php on line 46
  file         app/Imports/StockImport.php
 ------------ ---------------------------------

"Importing..."

 ------------ ---------------------------------
  date         Sun, 27 Jan 2019 02:37:21 +0000
  controller   "StockImportController"
  source       StockImport.php on line 46
  file         app/Imports/StockImport.php
 ------------ ---------------------------------

"Importing..."

 ------------ ---------------------------------
  date         Sun, 27 Jan 2019 02:37:21 +0000
  controller   "StockImportController"
  source       StockImport.php on line 46
  file         app/Imports/StockImport.php
 ------------ ---------------------------------

"Importing..."

 ------------ ----------------------------------------------------
  date         Sun, 27 Jan 2019 02:37:21 +0000
  controller   "StockImportController"
  source       StockImportController.php on line 48
  file         app/Http/Controllers/API/StockImportController.php
 ------------ ----------------------------------------------------

"End Import"

Additional Information

I read regarding import Multiple Sheets documentation. I not really understand.

 public function sheets(): array
    {
        return [
            // Select by sheet index
            0 => new FirstSheetImport(),

            // Select by sheet name
            'Other sheet' => new SecondSheetImport
        ];
    }

How's the nwe FirstSheetImport() definite? Can't see the relationship.

question

Most helpful comment

You create a new sheet import object and link that to sheet index 0 (= first worksheet in file).

public function sheets(): array
    {
        return [
            0 => new FirstSheetImport(),
        ];
    }

If you don't want to have 2 classes for this, you can also do a self reference. Then the import object will be re-used to only import the first worksheet.

public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }

I've updated the documentation with more explanation: https://laravel-excel.maatwebsite.nl/3.1/imports/multiple-sheets.html

All 4 comments

You create a new sheet import object and link that to sheet index 0 (= first worksheet in file).

public function sheets(): array
    {
        return [
            0 => new FirstSheetImport(),
        ];
    }

If you don't want to have 2 classes for this, you can also do a self reference. Then the import object will be re-used to only import the first worksheet.

public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }

I've updated the documentation with more explanation: https://laravel-excel.maatwebsite.nl/3.1/imports/multiple-sheets.html

public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }

work like charm. For my use case, it is perfectly match.

However by reading the documentation, I still have no idea what to put inside the FirstSheetImport(),?
Where to create this function? what to put inside? I hope can add more info in your documentation.

Quoted from docs:

A sheet import class can import the same concerns as a normal import object.

So in your case it would be: ToCollection

I have models for each worksheet. And excel is single file consist of a number of worksheet.
How can i get row count added to model for each worksheet?
Example a workheet like this;
$file = $request->file('summary_list'); $staff_workingday_import = new StaffSummaryImport($this->staff_repository->all(), $this->user_repository->getAuthUser()->id); $staff_workingday_import->onlySheets('脟al谋艧ma G眉nleri'); Excel::import($staff_workingday_import, $file); dd($staff_workingday_import->sheets('脟al谋艧ma G眉nleri')->getRowCount());

Was this page helpful?
0 / 5 - 0 ratings