


Laravel Development: How to import and export Excel files using Laravel Excel?
With the rapid development of the Internet, data processing is becoming more and more important, especially in enterprise data management. Excel files have become one of the essential tools for corporate offices because they can easily store, edit, calculate, and analyze data. Laravel is a widely used PHP framework, and Laravel Excel is an Excel file operation extension package developed for Laravel, which can easily import and export Excel files.
This article will introduce you to the use of Laravel Excel in detail. In this article, we will learn how to install Laravel Excel and import and export Excel files.
1. Install Laravel Excel
Install Laravel Excel through Composer in the terminal
composer require maatwebsite/excel
If you are using a Laravel version less than 5.5, you need to configure/app.php The following two service providers are configured in the file:
MaatwebsiteExcelExcelServiceProvider::class,
'Excel' => MaatwebsiteExcelFacadesExcel::class,
If you are using a Laravel version greater than or equal to 5.5, you do not need to manually add service providers, these service providers will be automatically added to the configuration.
Publish through the Artisan command in the terminal:
php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider"
This will automatically generate the following configuration files and template files:
config/excel.php resources/views/vendor/excel
2. Configure Laravel Excel
The configuration file excel.php contains all configuration options for Laravel Excel. These options can be defined directly in the .env file or configured in the config/excel.php file. Below is a detailed description of all possible options.
'default_driver' => 'local',
The option "default_driver" specifies the default driver to be used. There are two options here: local and ftp.
'cache' => [ 'enabled' => true, 'driver' => 'laravel', ],
The option "cache" specifies caching options. Caching can improve speed, especially when processing large amounts of data. When caching is enabled, set "cache_driver" to "laravel" or "memcached".
'temp_path' => sys_get_temp_dir(),
Options temp_path specifies the file system path to be used to save temporary files.
'csv' => [ 'delimiter' => ',', 'enclosure' => '"', 'escape_character' => '\', 'input_encoding' => 'UTF-8', 'output_encoding' => 'UTF-8', 'use_bom' => false, ],
The option "csv" allows configuring CSV import and export options, which mainly include the following options:
delimiter: delimiter, such as: comma, semicolon, Tab, etc.
enclosure: The enclosure symbol of the column.
escape_character: escape character.
input_encoding: Input encoding.
output_encoding: Output encoding.
use_bom: Whether to use Bom byte order.
'exports' => [ 'force_resave' => false, 'ignore_empty' => false, 'pre_calculate_formulas' => false, 'maximum_recursion' => 50, ],
The option "exports" allows configuring export options, mainly including the following options:
force_resave: whether to force saving.
ignore_empty: Whether to ignore empty cells.
pre_calculate_formulas: Whether to pre-calculate formulas.
maximum_recursion: The maximum number of levels of recursion.
3. Export Excel files
Laravel Excel provides good support for exporting Excel files. Next we will demonstrate how to use Laravel Excel for data export.
First, open the controller file, create a new Excel file and export the data:
<?php namespace AppHttpControllers; use MaatwebsiteExcelFacadesExcel; use AppExportsUsersExport; class ExportController extends Controller { public function export() { return Excel::download(new UsersExport, 'users.xlsx'); } }
In the above controller method, we use the Excel::download() method to create a Excel file. This method accepts two parameters:
- The first parameter users will be the exported data. We implement data export by creating a UsersExport auxiliary class with a toExcel() method.
- The second parameter is the file name, which is required.
The contents of the UsersExport file are as follows:
<?php namespace AppExports; use MaatwebsiteExcelConcernsFromCollection; use AppUser; class UsersExport implements FromCollection { public function collection() { return User::select('id', 'name', 'email')->get(); } }
This class needs to implement the MaatwebsiteExcelConcernsFromCollection interface. We can see that FromCollection returns a collection class, where the get() method will return all users stored in the class.
In this way, we have completed the export operation of Laravel Excel.
4. Import Excel files
Laravel Excel also provides very good support for the import operation of Excel files. Next we will demonstrate how to use Laravel Excel to import data from Excel files.
First, open the controller file and import the data in the Excel file into the database:
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppImportsUsersImport; class ImportController extends Controller { public function import(Request $request) { $file = $request->file('file'); Excel::import(new UsersImport, $file); return redirect('/')->with('success', 'Excel 数据已成功导入!'); } }
In the above controller method, we use the Excel::import() method to import the Excel file. This method accepts two parameters:
- The first parameter is UsersImport, which is a class that imports Excel files into the database.
- The second parameter is the Excel file to be imported, which contains the data to be imported.
Now, let's take a look at the UsersImport class:
<?php namespace AppImports; use MaatwebsiteExcelConcernsToModel; use AppUser; class UsersImport implements ToModel { public function model(array $row) { return new User([ 'name' => $row[0], 'email' => $row[1], 'password' => bcrypt($row[2]) ]); } }
As you can see, this class needs to implement the MaatwebsiteExcelConcernsToModel interface, which defines a model() method. This method will be used to determine the attributes of the new user.
In the model() method, we use the data row information of the array to store the user's attributes. Here we assume that the first line in the Excel file is the username, the second line is the email address, and the third line is the password.
This is how we use Laravel Excel to import and export Excel files. I believe you have learned about the basic usage and some advanced features of Laravel Excel. Hope this article is helpful to your Laravel learning.
The above is the detailed content of Laravel development: How to import and export Excel files using Laravel Excel?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于折叠表格的相关问题,就是分类汇总的功能,这样查看数据会非常的方便,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于AGGREGATE函数的相关内容,该函数用法与SUBTOTAL函数类似,但在功能上比SUBTOTAL函数更加强大,下面一起来看一下,希望对大家有帮助。

在之前的文章《实用Excel技巧分享:利用 数据透视表 来汇总业绩》中,我们学习了下Excel数据透视表,了解了利用数据透视表来汇总业绩的方法。而今天我们来聊聊怎么计算时间差(年数差、月数差、周数差),希望对大家有所帮助!

在之前的文章《实用Word技巧分享:聊聊你没用过的“行号”功能》中,我们了解了Word中你肯定没用过的"行号”功能。今天继续实用Word技巧分享,看看Excel表格怎么借用Word进行分栏打印,快来收藏使用吧!

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于zenmm制作倒计时牌的相关内容,使用Excel中的日期函数结合按指定时间刷新的VBA代码,即可制作出倒计时牌,下面一起来看一下,希望对大家有帮助。

在之前的文章《实用Excel技巧分享:原来“定位功能”这么有用!》中,我们了解了定位功能的妙用。而今天我们聊聊合并后的单元格如何实现筛选功能,分享一种复制粘贴和方法解决这个问题,另外还会给大家分享一种合并单元格的不错的替代方式。

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于如何使用函数寻找总和为某个值的组合的问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Excel的相关知识,其中主要介绍了关于XLOOKUP函数的相关知识,包括了常规查询、逆向查询、返回多列、自动除错以及近似查找等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
