10-Apr-2023
.
Admin
Hello friends,
In this tutorial, i am going to learn you how to export excel file using streaming in laravel. We will show streaming large excel file with laravel cursor. i will show you how to export excel file in laravel with cursor. if you want to export large number of data then you can use laravel cursor method to get data and streaming to export excel file.
If you want to get large amount of data without load then you can use laravel cursor method. Currenty i am working new project and i have a problem loading issue with exporting more than 10000 record then you can use bellow example.
This artcle will give you streaming export excel with cursor in laravel example. i will give you simple and easy way to export excel file in laravel.
Here i will give you full example for how to export excel file with streaming in laravel. So let's follow bellow step:
Step 1: Install Laravel Project
First, you need to download the laravel fresh setup. Use this command then download laravel project setup :
composer create-project --prefer-dist laravel/laravel blog
Step 2: Setup Database
After successfully install laravel 8 Application, Go to your project .env file and set up database credential and move next step :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name
DB_USERNAME=here database username
DB_PASSWORD=here database password
Step 3 : Add Route
In this step, you have to add two route for export excel file in laravel. first route for view and second one is export excel.
use App\Http\Controllers\ExcelExportController;
Route::get('export',[ExcelExportController::class, 'exportExcelView']);
Route::get('export-excel',[ExcelExportController::class, 'exportExcel'])->name('export.excel');
Step 4 : Create Controller
Now you can create new controller as ExcelExportController. in this controller you have to bellow add two method :
1)exportExcelView
2)exportExcel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class ExcelExportController extends Controller
{
public function exportExcelView()
{
return view('exportExcel');
}
public function exportExcel()
{
$name = 'users.xlsx';
$headers = [
'Content-Disposition' => 'attachment; filename='. $name,
];
$colom = \Illuminate\Support\Facades\Schema::getColumnListing("users");
$temp_colom = array_flip($colom);
unset($temp_colom['id']);
$colom = array_flip($temp_colom);
return response()->stream(function() use($colom){
$file = fopen('php://output', 'w+');
fputcsv($file, $colom);
$data = \App\Models\User::cursor();
foreach ($data as $key => $value) {
$data = $value->toArray();
unset($data['id']);
fputcsv($file, $data);
}
$blanks = array("\t","\t","\t","\t");
fputcsv($file, $blanks);
$blanks = array("\t","\t","\t","\t");
fputcsv($file, $blanks);
$blanks = array("\t","\t","\t","\t");
fputcsv($file, $blanks);
fclose($file);
}, 200, $headers);
}
}
Step 5 : Create Blade File
In this step, you have to create exportExcel.blade.php file and put bellow code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Streaming Export Excel in Laravel 8</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-6 offset-3">
<div class="card">
<div class="card-header">
<strong>Streaming Export Excel in Laravel 8</strong>
</div>
<div class="card-body">
<a href="{{ route('export.excel') }}" class="btn btn-success">Export Excel</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Now we are ready to run streaming export excel with cursor in laravel so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
localhost:8000/export
I hope it will help you...
#Laravel 8
#Laravel 7
#Laravel
#Laravel 6