How to Export data to Excel/CSV file in Laravel 10
Export data to Excel with different format like .xlsx , .xls, csv,
We will learn how to export data in Excel file with different format from a database table in Laravel 10.
We will download the Excel file in different Format / Extension like (.xlsx , .xls, csv, etc.),
Let’s get started.
Step 1: Install Laravel Excel Package via composer as follows:
composer require maatwebsite/excel
Step 2: Configure the Package
The Maatwebsite\Excel\ExcelServiceProvider is auto-discovered and registered by default.
If you want to it yourself, add the ServiceProvider in “config/app.php”: (aliases & providers )
'providers' => [
// ...
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
// ...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
Step 3: To publish the config, run the vendor publish command
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
This command is to create a new config file config/excel.php
Step 4: Use this below in Form to export / download the excel file as per your requirement.
<form action="{{ url('user/export') }}" method="POST">
<label>Export User Data in Excel File</label>
<div class="input-group mt-2">
<select name="type" class="form-control" required>
<option value="">Select Excel Format</option>
<option value="xlsx">XLSX</option>
<option value="csv">CSV</option>
<option value="xls">XLS</option>
</select>
<button type="submit" class="btn btn-success">Export</button>
</div>
</form>
Step 5: Create a Route
Route::POST('user/export', [App\Http\Controllers\UserController::class, 'exportData']);
Step 6: Create a Controller named UserController using the below artisan command:
php artisan make:controller UserController
After successfully creating the UserController, paste the below code in it.
In this controller, write the exporting logic and code how to export in different format
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\UserExport;
use Maatwebsite\Excel\Facades\Excel;
class UserController extends Controller
{
public function exportData(Request $request)
{
if($request->type == 'xlsx'){
$fileExt = 'xlsx';
$exportFormat = \Maatwebsite\Excel\Excel::XLSX;
}
elseif($request->type == 'csv'){
$fileExt = 'csv';
$exportFormat = \Maatwebsite\Excel\Excel::CSV;
}
elseif($request->type == 'xls'){
$fileExt = 'xls';
$exportFormat = \Maatwebsite\Excel\Excel::XLS;
}
else{
$fileExt = 'xlsx';
$exportFormat = \Maatwebsite\Excel\Excel::XLSX;
}
$filename = "user-".date('d-m-Y').".".$fileExt;
return Excel::download(new UserExport, $filename, $exportFormat);
}
}
Step 7: Create a Excel Export Class named UserExport with the following command:
php artisan make:export UserExport
After successfully creating the Import Class, paste the below code in the following path : app/ Exports/ UserExport.php
There are different ways to Export data to excel file like:
1. Export Data in Excel Sheet with Headings (Column Names)
?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class UserExport implements FromCollection, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::select('name','email','phone')->get();
}
public function headings(): array
{
return [
'Name',
'Email',
'Phone',
];
}
}
2. Export Data in Excel Sheet without Headings
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UserExport implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
3. Export Data to Excel Sheet from a “Blade View” using “FormView” concern
<?php
namespace App\Exports;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class UserExport implements FromView
{
public function view(): View
{
return view('user.export', [
'user' => User::all()
]);
}
}
Now, while export data to an excel file from a “Blade view” using a FormView concern, you have to create a Blade file in the following path: resources/ views/ user/ export.blade.php and paste the below code:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
@foreach($user as $userData)
<tr>
<td>{{ $userData->name }}</td>
<td>{{ $userData->email }}</td>
<td>{{ $userData->phone }}</td>
</tr>
@endforeach
</tbody>
</table>
Click here to learn more Thanks for reading.