Importing Only the First Sheet with Laravel Excel

Laravel Excel Import: Importing Only the First Sheet

Laravel Excel is a powerful package that allows you to easily import and export Excel files in Laravel applications. By default, when importing an Excel file with multiple sheets, Laravel Excel imports all the sheets. However, there may be cases when you only need to import the first sheet. In this article, we will explore how to import only the first sheet using Laravel Excel.

Step 1: Install Laravel Excel

Before we begin, make sure you have Laravel Excel installed in your Laravel project. You can install Laravel Excel using Composer by running the following command:

composer require maatwebsite/excel

Step 2: Create an Import Class

Next, we need to create an import class that extends the MaatwebsiteExcelConcernsToModel class. This class will handle the import logic for us. Let’s create a new class called FirstSheetImport using the following command:

php artisan make:import FirstSheetImport

This will create a new import class under the AppImports namespace. Open the FirstSheetImport class and modify it as follows:

namespace AppImports;

use MaatwebsiteExcelConcernsToModel;
use MaatwebsiteExcelConcernsWithStartRow;

class FirstSheetImport implements ToModel, WithStartRow
{
    public function model(array $row)
    {
        // Import logic for each row
    }

    public function startRow(): int
    {
        return 2; // Skip the header row
    }
}

In the FirstSheetImport class, we implement the ToModel interface, which requires us to define the model method. This method will be called for each row in the Excel sheet. You can write your import logic inside this method.

Additionally, we implement the WithStartRow interface and define the startRow method. This method allows us to skip the header row when importing the sheet. In this example, we skip the first row by returning 2.

Step 3: Import Only the First Sheet

Now that we have our import class ready, we can use it to import only the first sheet from an Excel file. In your controller or wherever you want to import the file, use the following code:

use AppImportsFirstSheetImport;
use MaatwebsiteExcelFacadesExcel;

public function import()
{
    $path = storage_path('app/file.xlsx');

    $import = new FirstSheetImport();
    Excel::import($import, $path);

    // Import completed
}

In the above code, we first specify the path to the Excel file we want to import. You may need to modify this path based on your project’s file structure.

Next, we create an instance of our FirstSheetImport class and pass it to the Excel::import method along with the file path. This will import only the first sheet from the Excel file.

Finally, you can add any additional logic or processing after the import is completed.

Conclusion

Laravel Excel provides a convenient way to import Excel files in Laravel applications. In this article, we learned how to import only the first sheet from an Excel file using Laravel Excel. By creating a custom import class and implementing the necessary interfaces, we were able to import only the desired sheet.

Remember to always refer to the official Laravel Excel documentation for more information and advanced usage. Happy importing!

Оцените статью