Laravel 11 Import Export Excel and CSV File Tutorial | websolutioncode.com
Laravel 11 Import Export Excel and CSV File Tutorial | websolutioncode.com

Laravel 11 Import Export Excel and CSV File Tutorial

Introduction

In this tutorial, we will explore how to import and export Excel and CSV files in a Laravel 11 application. Handling Excel and CSV files is a common requirement in web applications, especially when dealing with data manipulation and management. Laravel provides convenient methods and packages to handle file import and export operations efficiently.

Prerequisites

Before we begin, make sure you have the following:

  1. Basic knowledge of Laravel framework.
  2. Composer installed on your machine.
  3. Laravel 11 project set up and running.

Step 1: Install Laravel Excel Package

First, we need to install the maatwebsite/excel package, which simplifies Excel and CSV file operations in Laravel applications. Open your terminal and navigate to your Laravel project directory. Run the following command:

composer require maatwebsite/excel

Step 2: Configuration

Once the package is installed, we need to configure it. Open the config/app.php file and add the following line to the providers array:

Maatwebsite\Excel\ExcelServiceProvider::class,

Next, add the following line to the aliases array:

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

Step 3: Create Controller

Now, let’s create a controller to handle import and export operations. Run the following command in your terminal:

php artisan make:controller FileController

This command will generate a new controller named FileController.

Step 4: Implement Import and Export Methods

Open the FileController class located at app/Http/Controllers/FileController.php. Implement the following methods:

use Excel;

class FileController extends Controller
{
    public function import(Request $request)
    {
        Excel::import(new YourImportClass, $request->file('file'));

        return redirect()->back()->with('success', 'File imported successfully!');
    }

    public function export()
    {
        return Excel::download(new YourExportClass, 'filename.xlsx');
    }
}

Replace YourImportClass and YourExportClass with the classes you create to handle import and export functionality, respectively.

Step 5: Create Import and Export Classes

Now, let’s create the import and export classes. Run the following commands in your terminal:

php artisan make:import YourImportClass
php artisan make:export YourExportClass

Open the generated import and export classes and define the necessary logic to handle data import and export operations.

Step 6: Create Routes

Finally, let’s define routes to access our import and export methods. Open the routes/web.php file and add the following routes:

Route::post('/import', 'FileController@import')->name('import');
Route::get('/export', 'FileController@export')->name('export');

Step 7: Create Views

To provide a user interface for importing and exporting files, let’s create some views. Create a new folder named files inside the resources/views directory. Inside the files folder, create two blade files named import.blade.php and export.blade.php.

import.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Import File</title>
</head>
<body>
    <form action="{{ route('import') }}" method="post" enctype="multipart/form-data">
        @csrf
        <input type="file" name="file">
        <button type="submit">Import</button>
    </form>
</body>
</html>

export.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Export File</title>
</head>
<body>
    <a href="{{ route('export') }}">Export Data</a>
</body>
</html>

Step 8: Test the Application

Now you can test your application. Visit the /import route to import a file, and visit the /export route to export data.

Congratulations! You have successfully implemented import and export functionality for Excel and CSV files in your Laravel 11 application.

Conclusion

In this tutorial, we learned how to handle Excel and CSV file import and export operations in a Laravel 11 application using the maatwebsite/excel package. Importing and exporting data is an essential feature for many web applications, and Laravel provides convenient methods to accomplish this task efficiently.

Leave a Reply