9

Import and Export Excel file in Laravel 7

 1 year ago
source link: https://www.laravelcode.com/post/import-and-export-excel-file-in-laravel-7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Import and Export Excel file in Laravel 7

  248816 views

  2 years ago

Laravel

import export excel or CSV from a database is a primary requisite for the admin project. In this tutorial, i will show you how to import CSV or excel file and export CSV or excel file utilizing maatwebsite/excel version 3 in laravel 6 application.

In this export excel or CSV from database tutorial, we will simple engender import data to CSV, Xls file and withal we can import data to the database utilizing CSV file in laravel 6 application.

In this tutorial, we will utilize maatwebsite/excel composer package for import and export tasks. maatwebsite/excel provide a facile way to import and export utilizing database model. maatwebsite/excel updated to version 3 and they provide a great way to import-export data from database, so first follow a few steps to get an example.

Today i am coming with an incipient tutorial. Today i will edify you how to import and export CSV file in laravel 7. In this example, i will utilize maatwebsite/excel composer package for import and export tasks.

This package is awe-inspiring to export and import CSV files in laravel applications. If you don't ken how to upload export and import CSV file in laravel application. I will edify you step by step. So don't worry.

So let's commence our today's tutorial export and import CSV file in laravel 7 application.

Step 1 : Install Laravel 6 Project

Here, we need install Laravel 6 application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Maatwebsite Package

In this step we need to install Maatwebsite package via the Composer package manager, so one your terminal and fire bellow command:

composer require maatwebsite/excel

Now open config/app.php file and add service provider and aliase.

config/app.php

'providers' => [

	....

	Maatwebsite\Excel\ExcelServiceProvider::class,

],

'aliases' => [

	....

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

],

Step 3: Add Routes

Route::get('export', 'DemoController@export')->name('export');
Route::get('importExportView', 'DemoController@importExportView');
Route::post('import', 'DemoController@import')->name('import');

Step 4: Create Import Class

In maatwebsite 3 version provide way to built import class and we have to use in controller. So it would be great way to create new Import class. So you have to run following command and change following code on that file:

php artisan make:import UsersImport --model=User

app/Imports/UsersImport.php

namespace App\Imports;
   
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
    
class UsersImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        return new User([
            'name'     => $row['name'],
            'email'    => $row['email'], 
            'password' => \Hash::make($row['password']),
        ]);
    }
}

Step 5: Create Export Class

maatwebsite 3 version provide way to built export class and we have to use in controller. So it would be great way to create new Export class. So you have to run following command and change following code on that file:

php artisan make:export UsersExport --model=User

app/Exports/UsersExport.php 

namespace App\Exports;
  
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
  
class UsersExport implements FromCollection
{
   
    public function collection()
    {
        return User::all();
    }
}

In this step, now we should create new controller as MyController in this path "app/Http/Controllers/DemoController.php". this controller will manage all importExportView, export and import request and return response, so put bellow content in controller file: To create this controller : run 

php artisan make:controller DemoController

app/Http/ControllersDemoController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Exports\UsersExport;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
  
class DemoController extends Controller
{
    
    public function importExportView()
    {
       return view('import');
    }
   
    public function export() 
    {
        return Excel::download(new UsersExport, 'users.xlsx');
    }
   
    public function import() 
    {
        Excel::import(new UsersImport,request()->file('file'));
           
        return redirect()->back();
    }
}

Step 7:  Create Blade File

In Last step, let's create import.blade.php(resources/views/import.blade.php) for layout and we will write design code here and put following code:

resources/views/import.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Import Export Excel to database Example</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
   
<div class="container">
    <div class="card bg-light mt-3">
        <div class="card-body">
            <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <input type="file" name="file" class="form-control">
                <br>
                <button class="btn btn-success">Import User Data</button>
                <a class="btn btn-warning" href="{{ route('export') }}">Export User Data</a>
            </form>
        </div>
    </div>
</div>
   
</body>
</html> 

i hope you like this article.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK