Laravel is a simple and elegant PHP Web development framework (PHPWebFramework ). It can free you from the messy code like noodles; it can help you build a perfect web APP, next let's take a look at the laravelexcel package tutorial Laravel is a simple and elegant PHP Web development Framework (PHP Web Framework ). It can free you from the messy code like a noodle; it can help you build a perfect web APP. Let's take a look at the laravel excel package tutorial.
Script ec (2); script
Installation of excel plug-in laravel
Introduce laravel excel package in composer
"Maatwebsite/excel": "1 .*"
Edit the app. php file in laravel/app/config and add the following values to the providers array:
'Maatwebsite \ Excel \ excelserviceprovider ',
Add the following values to the aliasses array in the same file:
'Excel '=> 'maatwebsite \ Excel \ Facades \ Excel ',
Run the composer install or composer update command.
Laravel excel Configuration
Some configuration items for the plug-in located in laravel/vendor/maatwebsite/excel/src/config
Config. php> global settings for excel and tables
Csv. php> Settings for importing and exporting csv files
Export. pho> some settings for printing the File Content
Import. php> Settings for importing excel files
Simple use of laravel excel
After all the preparations have been completed, we can use the excel plug-in.
Export excel
$ Rows = array ('id' => 1, 'name' => 'marlon '));
Excel: create ($ name, function ($ excel) use ($ rows ){
$ Excel-> sheet ('register today ', function ($ sheet) use ($ rows ){
$ Sheet-> fromArray ($ rows );
});
})-> Store ('xls ', storage_path ('excel '));
Because the variables outside the closure cannot be obtained in php closures, you need to use to introduce $ rows, the parameters passed in the store in the last chained call are the format of the required excel file and the location to be saved to the server. This is the absolute path.
In this place, the store () method is storage, and the corresponding download () method can also be used for direct download. As for the export method, I have not understood what it is useful.
Import excel
Excel: load (Input: file ('excel '), function ($ reader ){
// Obtain the excel worksheet
$ Reader = $ reader-> getSheet (0 );
// Obtain table data
$ Results = $ reader-> toArray ();
// $ Results is already the data in excel here. You can operate on it here, store it in the database or other ....
});