dan3460's avatar

Upload specific sheet from Excel in LaravelExcel

I'm testing the possibility of uploading data from 2 sheets from an excel file. The two sheets contain different data, so they need to be considered as different imports. The instructions that i fund at https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html are a little confusing. Does anyone have a practical example on how to do this? Thanks for the help.

0 likes
6 replies
dan3460's avatar

Yes, each sheet is going to a different table. I read the documentation that is why i was asking for a practical example. I'm confused where the FirstSheetImport class was created. All the practical examples that i found in the web uses the same one sheet sample of uploading users to the user table from a one sheet excel.

lemmon's avatar
lemmon
Best Answer
Level 28

@dan3460

I have a excel spreadsheet with 2 sheets. names and products

You need a class for each sheet. NamesImport and ProductsImport

You need another class to assign sheet names to each import NamesProductsImport

I have a database with 2 tables names and products.

<?php

namespace App\Imports;

use App\Models\Name;
use Maatwebsite\Excel\Concerns\ToModel;

class NamesImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Name([
            'first-name' => $row[0],
            'last-name' => $row[1],
        ]);
    }
}
<?php

namespace App\Imports;

use App\Models\Product;
use Maatwebsite\Excel\Concerns\ToModel;

class ProductsImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Product([
            'company' => $row[0],
            'quantity' => $row[1],
        ]);
    }
}

Now you need a class to run each import as a separate sheet notice the sheet names.

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class NamesProductsImport implements WithMultipleSheets
{
    public function sheets(): array
    {
        return [
            'names' => new NamesImport(),
            'products' => new ProductsImport(),
        ];
    }
}

The controller calls the NamesProductsImport. that calls each separate import by sheet.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Imports\NamesProductsImport;
use Maatwebsite\Excel\Facades\Excel;

class NamesProductsController extends Controller
{
    public function import()
    {
        Excel::import(new NamesProductsImport, base_path('names_products.xlsx'));

        return redirect('/')->with('success', 'All Good!');
    }
}

here are the migrations

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNamesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('names', function (Blueprint $table) {
            $table->id();
            $table->string('first-name');
            $table->string('last-name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('names');
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('company');
            $table->integer('quantity');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

works like a charm.

dan3460's avatar

Thanks very much. appreciate the help.

joseMendoza's avatar

@lemmon I have a problem when using multiple sheets, as it cannot get the fairules of the NamesProductsImport class from, for example, NamesImport.

Please or to participate in this conversation.