Other

How to Export data to Excel file in Laravel

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: 

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 )

Step 3: To publish the config, run the vendor publish command

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. 

Step 5: Create a Route

Step 6: Create a Controller named UserController using the below artisan command:

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

Step 7: Create a Excel Export Class named UserExport with the following command:

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)

2. Export Data in Excel Sheet without Headings

3. Export Data to Excel Sheet from a “Blade View” using “FormView” concern 

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:  

Click here to learn more

Thanks for reading.

Visited 12 times, 1 visit(s) today

Leave a Reply

Your email address will not be published. Required fields are marked *