Выгрузка в excel laravel
In this tutorial, we will see Laravel 7/8 Export In Excel and CSV Data Example using the maatwebsite/excel 3.1 packages. In the general Laravel project, we always need to import or export the rows from the database. The maatwebsite/excel makes it very easy to import-export data in Laravel.
Overview of Export in Laravel
- Step 1: Install the maatwebsite/excel package using the composer.
- Step 2: Configure the package.
- Step 3: Create a model and migration files.
- Step 4: Create a controller and routes.
- Step 5: Create a view file, and inside the file, create an HTML form.
- Step 6: Create a view file to display the data to the user.
- Step 7: Create an export class.
- Step 8: Write the export function inside the controller file.
Export Data In Excel and CSV in Laravel 7/8
If you want to up and running with basic laravel functionality, go to my other article on this web blog called Laravel 7 Crud Example From Scratch.
If you want to Generate PDF In Laravel, then check out Laravel Generate PDF From View Example. For this example, we use the package called maatwebsite/excel version 3.1. So, our Laravel 8/7 and maatwebsite/excel 3.1.
When using the package in your application, it’s good to understand how it functions behind the scenes. Following the behind-the-scenes will make you feel more comfortable and confident using the maximum potential of the tool.
Laravel Excel 3.1
? Laravel Excel is intended at being Laravel-flavoured PhpSpreadsheet: a simple but elegant wrapper around PhpSpreadsheet to simplify the exports and imports.
? PhpSpreadsheet is the library written in pure PHP and providing the set of classes that allow us to read from and write to different types of spreadsheet file formats, like Excel and LibreOffice Calc.
Laravel Excel Features
- We can easily export collections to Excel.
- We can export queries with automatic chunking for better performance.
- We can queue exports for better performance.
- We can easily export Blade views to Excel.
- We can easily import it to collections.
- We can read the Excel file in chunks.
- We can handle the import inserts in batches.
Requirements
- PHP: ^7.0
- Laravel: ^5.5
- PhpSpreadsheet: ^1.6
- PHP extension php_zip enabled
- PHP extension php_xml enabled
- PHP extension php_gd2 enabled
Step 1: Installation
Require the following package in the composer.json of your Laravel 8 project. The following command will download the package and PhpSpreadsheet.
Step 2: Configure package
The Maatwebsite\Excel\ExcelServiceProvider is auto-discovered and registered by default.
If you want to register by yourself, then add the ServiceProvider in config/app.php :
Excel facade is auto-discovered.
If you want to add it manually, add a Facade in config/app.php :
If you want to publish a config, run the vendor publish command:
This will create the new config file named config/excel.php .
Step 3: Create model and migration files
Type the following command.
Now, go to the [timestamp].create_disneypluses_table.php file and add the columns.
Now, migrate the database using the following command.
Step 4: Create a controller and routes
The next step is to create a DisneyplusController.php file.
Now, add the two routes inside the routes >> web.php file.
Now, create two methods inside the DisneyplusController.php file.
Step: 5 Create a form blade file for input the data
Now, inside the views folder, create one file called the form.blade.php file. Add the following code.
Step 6: Store data in the database
Now, we will write the two functions inside the DisneyplusController.php file.
So, in the above file, first, we have shown the form file, and then inside the store function, we check for validation and then store the data into the database.
Also, add the fillable fields inside the Disneyplus.php model file.You will see one form. Try to save the data, and if everything in the code is right, you will see one entry in the database.
Step: 7 Create a view file to display the data.
Before we create a view file, we need to add one route inside the web.php.
Now, create a view file called list.blade.php file. Add the following code.
Now, add the code inside the index() function of the DisneyplusController.php file.
You will see the listing of the shows.
Step 8: Create Exports class
You may do this by using the make:export command.
The file can be found in app/Exports directory.
The file DisneyplusExport.php is the following.
If you prefer to create the export manually, you can build the following in app/Exports .
Step 9: Write the export function
Inside the DisneyplusController.php file, add the following code.
So, our final file looks like below.
Finally, add the route to be able to access the export:
Also, add the link to the Export inside the list.blade.php file.
Click on the Export link, and you will see the disney.xlsx file inside your Download folder.
Exporting collections in CSV in Laravel
By default, the export format is determined by the extension of the file.
It will download the CSV file.
If you want to configure the export format explicitly, you can pass it through as 2nd parameter.
You can find more details about export in different formats on this link.
Supercharged Excel exports and imports
A simple, but elegant Laravel wrapper around PhpSpreadsheet exports and imports.
Easily export collections to Excel. Supercharge your Laravel collections and export them directly to an Excel or CSV document. Exporting has never been so easy.
Supercharged exports. Export queries with automatic chunking for better performance. You provide us the query, we handle the performance. Exporting even larger datasets? No worries, Laravel Excel has your back. You can queue your exports so all of this happens in the background.
Supercharged imports. Import workbooks and worksheets to Eloquent models with chunk reading and batch inserts! Have large files? You can queue every chunk of a file! Your entire import will happen in the background.
Export Blade views. Want to have a custom layout in your spreadsheet? Use a HTML table in a Blade view and export that to Excel.
🎓 Learning Laravel Excel
You can find the full documentation of Laravel Excel on the website.
Laravel Excel is created with love and care by Spartner (formerly known as Maatwebsite) to give back to the Laravel community. It is completely free (MIT license) to use, however the package is licensed as Postcardware. This means that if it makes it to your production environment, we would very much appreciate receiving a postcard from your hometown.
Spartner
Markt 2
6231 LS Meerssen
The Netherlands.
Created by Spartner (formerly Maatwebsite)
We are a strategic development partner, creating web-based custom built software from Laravel. In need of a digital solution for your challenge? Give us a call.
Laravel Excel v2.3.* for Laravel 5.x, 6.x, 7.x and 8.x
Laravel Excel brings the power of PHPOffice's PHPExcel to Laravel 5 with a touch of the Laravel Magic. It includes features like: importing Excel and CSV to collections, exporting models, array's and views to Excel, importing batches of files and importing a file by a config file.
- Import into Laravel Collections
- Export Blade views to Excel and CSV with optional CSS styling
- Batch imports
- A lot of optional config settings
- Easy cell caching
- Chunked and queued importer
- ExcelFile method injections
- Editing existing Excel files
- Advanced import by config files
- and many more.
Require this package in your composer.json and update composer. This will download the package and PHPExcel of PHPOffice.
In Laravel 5.5 or higher, this package will be automatically discovered and you can safely skip the following two steps.
If using Laravel 5.4 or lower, after updating composer, add the ServiceProvider to the providers array in config/app.php
You can use the facade for shorter code; if using Laravel 5.4 or lower, add this to your aliases:
The class is bound to the ioC as excel
To publish the config settings in Laravel 5 use:
This will add an excel.php config file to your config folder.
Support only through Github. Please don't mail us about issues, make a Github issue instead.
ALL bug fixes should be made to appropriate branch (e.g. 2.0 for 2.0.* bug fixes). Bug fixes should never be sent to the master branch.
This package is licensed under MIT. You are free to use it in personal and commercial projects. The code can be forked and modified, but the original copyright author should always be included!
Supercharged Excel exports and imports
A simple, but elegant Laravel wrapper around PhpSpreadsheet exports and imports.
Easily export collections to Excel. Supercharge your Laravel collections and export them directly to an Excel or CSV document. Exporting has never been so easy.
Supercharged exports. Export queries with automatic chunking for better performance. You provide us the query, we handle the performance. Exporting even larger datasets? No worries, Laravel Excel has your back. You can queue your exports so all of this happens in the background.
Supercharged imports. Import workbooks and worksheets to Eloquent models with chunk reading and batch inserts! Have large files? You can queue every chunk of a file! Your entire import will happen in the background.
Export Blade views. Want to have a custom layout in your spreadsheet? Use a HTML table in a Blade view and export that to Excel.
🎓 Learning Laravel Excel
You can find the full documentation of Laravel Excel on the website.
Laravel Excel is created with love and care by Spartner (formerly known as Maatwebsite) to give back to the Laravel community. It is completely free (MIT license) to use, however the package is licensed as Postcardware. This means that if it makes it to your production environment, we would very much appreciate receiving a postcard from your hometown.
Spartner
Markt 2
6231 LS Meerssen
The Netherlands.
Created by Spartner (formerly Maatwebsite)
We are a strategic development partner, creating web-based custom built software from Laravel. In need of a digital solution for your challenge? Give us a call.
Читайте также: