Php framework laravelexcel package tutorial-PHP source code

Source: Internet
Author: User
Tags php web development composer install
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 ....
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.