#ImportExportExcel&CSV
Explore tagged Tumblr posts
codesolutionsstuff · 3 years ago
Text
Laravel 8 Import Export Excel & CSV File Example
Tumblr media
Laravel Excel is intended to be a PHPSpreadsheet with a Laravel flavour. It is a simple and elegant wrapper for PhpSpreadsheet that simplifies exports and imports. PhpSpreadsheet is a php-based library that allows you to read and write spreadsheet file formats such as Excel and LibreOffice Calc. The following features are available in Laravel Excel:
Laravel Excel Features
- Collections can be easily exported to Excel. - For improved performance, export queries with automatic chunking. - Export queues for improved performance. - Export Blade views to Excel with ease. - Import to collections is simple. - Reading the Excel file in chunks is recommended. - Import inserts should be handled in batches. This laravel maatwebsite/excel tutorial is ideal if you want to create simple import and export, excel file functionality. By the end of this tutorial, you will be able to download and import Excel and CSV files directly from the database in the Laravel application.
Step 1: Install Laravel Project
To begin, launch Terminal and enter the following command to create a new Laravel project: composer create-project --prefer-dist laravel/laravel laravel-excel Alternatively, if you have the Laravel Installer installed as a global composer dependency: laravel new laravel-excel
Step 2: Configure Database Details
Following that, installation Go to the project's root directory, open the.env file, and configure the database as follows: DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE= DB_USERNAME= DB_PASSWORD=
Step 3: Install maatwebsite/excel package
You can install Laravel Excel using composer. This command must be executed in order for the installation to take place. composer require maatwebsite/excel You can include the following code in the config/app.php file. 'providers' => , 'aliases' => , Execute the vendor, then publish the command and configuration. php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider" --tag=config This will generate a new configuration file called config/excel.php.
Step 4: Generate Fake Data and Migrate Table
The user table is migrated in the first step. We proceeded to the second step after the migration was completed successfully. php artisan migrate The fake record is created in the second step. Tinker is used to generate the fake records in this case. You can use a different method depending on your needs. php artisan tinker You must run this command after opening the tinker to generate the fake records in our database. User::factory()->count(100)->create();
Step 5: Create a Routes
In this step, we will create a route to handle file import and export requests. use AppHttpControllersUserController; Route::get('/file-import',)->name('import-view'); Route::post('/import',)->name('import'); Route::get('/export-users',)->name('export-users');
Step 6: Create Import Class
Maatwebsite provides a method for creating an import class, which we must use in the controller. As a result, it would be an excellent way to create a new Import class. As a result, you must execute the following command and modify the following code in that file: php artisan make:import ImportUser --model=User app/Imports/ImportUser.php Read the full article
0 notes