Laravel 10 Import and Export File using Fast Excel

Hey there! In this guide, I'll walk you through the seamless process of importing and exporting files in Laravel 10 using the powerful Fast Excel package. You can import and export CSV and Excel files in Laravel 8, Laravel 9, and Laravel 10 using Fast Excel.

Discover how to effortlessly manage data, perform CSV imports and exports, and unlock the full potential of Excel functionalities within your Laravel applications. 

Let's dive in and make file operations a breeze in Laravel 9 and Laravel 10 Web Development!

Step 1: Install Laravel Project

If you haven't already, create a new Laravel project using Composer:

composer create-project --prefer-dist laravel/laravel your-project-name

 

Step 2: Install Fast Excel

Install the Rap2hpoutre's Fast Excel package using Composer:

composer require rap2hpoutre/fast-excel

 

Step 3: Set up your Model

Create a model for the data you want to import/export. This model will represent the structure of your data. For example:

php artisan make:model Product -m

Define your model's attributes in the migration file.

 

Step 4: Database Migration

Run the migration to create the necessary table in your database:

php artisan migrate

 

 

Step 5: Create Routes

Define routes in the web.php or api.php file for importing and exporting:

use App\Http\Controllers\ExcelController;

Route::get('/import-export', [ExcelController::class, 'showImportExportView']);
Route::get('/export', [ExcelController::class, 'export'])->name('export');
Route::post('/import', [ExcelController::class, 'import'])->name('import');

 

Step 6: Create Controller

Generate a controller using the following command:

php artisan make:controller ExcelController

Open the generated ExcelController and add methods for exporting and importing.

 

Step 7: Export Method

In your ExcelController, create a method to export data:

use Rap2hpoutre\FastExcel\FastExcel;

public function showImportExportView()
{
    return view('import-export');
}

public function export()
{
    $data = YourModel::all();
    return (new FastExcel($data))->download('export.xlsx');
}

 

Step 8: Import Method

Add a method to handle the import:

use Rap2hpoutre\FastExcel\FastExcel;

public function import(Request $request)
{
    $file = $request->file('import_file');

    $importedData = (new FastExcel)->import($file, function ($line) {
        YourModel::create([
            'column1' => $line['column1'],
            'column2' => $line['column2'],
            // Add other columns as needed - techsolutionstuff
        ]);
    });

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

 

Step 9: Create Blade Views

Create Blade views for the import and export functionalities.

resources/views/import-export.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Data Import and Export - Techsolutionstuff</title>
</head>
<body>
    <h2>Data Import and Export - Techsolutionstuff</h2>

    <!-- Export Section -->
    <section>
        <h3>Export Data</h3>
        <form action="{{ route('export') }}" method="get">
            @csrf
            <button type="submit">Export Data</button>
        </form>
    </section>

    <hr>

    <!-- Import Section -->
    <section>
        <h3>Import Data</h3>

        @if(session('success'))
            <p style="color: green;">{{ session('success') }}</p>
        @endif

        <form action="{{ route('import') }}" method="post" enctype="multipart/form-data">
            @csrf
            <input type="file" name="import_file" accept=".xlsx, .csv" required>
            <button type="submit">Import Data</button>
        </form>
    </section>
</body>
</html>

 

Step 10: Test Your Application

Run your Laravel development server:

php artisan serve

 

Conclusion:

Mastering file import and export in Laravel 10 with Fast Excel has never been easier in wrapping up. 

We've learned how to handle data effortlessly, perform CSV imports and exports, and tap into the robust Excel capabilities that Laravel offers.

Happy coding!

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS