Laravel Export Plugin

Source: Internet
Author: User

Forwards: 79296470

Version: Laravel5

PHP 5.6

Installation steps:

First, install the plug-in

①, first use the composer installation dependency in the Laravel project root directory:

Composer require "maatwebsite/excel:~2.1.0"

②, registering the service provider to the providers array in config/app.php:

Maatwebsite\excel\excelserviceprovider::class,

③, registering the façade to the aliases array in config/app.php:

' Excel ' = maatwebsite\excel\facades\excel::class,

④, we recommend that you generate a laravel Excel configuration file using the following command:

PHP artisan vendor:publish--provider="Maatwebsite\excel\excelserviceprovider"

Then you will find a excel.php file in the Config directory
You can open a look at some of the configuration items, mainly cache, form, and import, export some settings.

Second, use

①, creating routes:

Excel Export Route::get ('/excel/export ',' member\[email protected] ')->name ('/excel/export ');

Excel import Route::get ('/excel/import ',' member\[email protected] ')->name ('/excel/import ');

1-1, the method of export:

<?phpnamespace App\http\controllers\member; UseApp\http\controllers\basecontroller; UseApp\model\member\memberfollow; Useilluminate\http\request; Useilluminate\database\queryexception; UseExcel;classMembercontrollerextendsbasecontroller{/** * * Excel Export*/     Public functionExport () {Ini_set(' Memory_limit ', ' 500M '); Set_time_limit(0);//set a time-out limit of 0 minutes        $cellData= Memberfollow::select (' xt_name ', ' sex ', ' face ')->limit (5)->get ()ToArray (); $cellData[0] =Array(' nickname ', ' Gender ', ' Avatar ');  for($i= 0;$i<Count($cellData);$i++){            $cellData[$i] =array_values($cellData[$i]); $cellData[$i][0] =Str_replace(' = ', '. ') = ',$cellData[$i][0]); }        //DD ($cellData);Excel::create (' User information ',function($excel) Use($cellData){            $excel->sheet (' Score ',function($sheet) Use($cellData){                $sheet->rows ($cellData);        }); })->export (' xls ');  die; }}

Let me explain the above code.
A. Ini_set setting the memory overflow size and timeout is because I have a large amount of data, do not want to go directly to the php.ini to modify, so directly in this setting. You can also modify the cache size in the excel.php configuration item and adjust it appropriately.
B. First you need to know that Celldata is a two-dimensional array, and that each one-dimensional array in a two-dimensional array must be an indexed array before it can be properly formatted.
So, I have toarray () conversion operation for the $celldata I have queried. Then I let you look at the structure of the query, because each column in our array table is a field, so it is decided that a one-dimensional array is an associative array, which needs to be processed by the key.

For loop processing is:

Another in the For loop
php
$cellData[$i][0] = str_replace(‘=‘,‘ ‘.‘=‘,$cellData[$i][0]);

This place is a bit of a pit, because Excel cell when the first character of a column in your exported data is the equals sign "=", he will do the calculation and then the error will be made. For example, I have a nickname that is "= Sunshine" everywhere, I'm just equal to the equals sign match the space
In fact, this does not need to match the replacement, because there are configuration items in the excel.php file, but I configured does not take effect, follow-up to find a better solution will be updated in a timely manner, or who knows can be pointed out.

/*        |--------------------------------------------------------------------------        | Calculate        |--------------------------------------------------------------------------        |        | By default cells with formulas would be calculated.        |        */        ' calculate ' and               false,---------------------Kukakawa Source: CSDN Original: 79296470?utm_source=copy copyright notice: This article is the original article of Bo Master, Reprint please attach the blog link!

  

By default, the cells that calculate the formula are evaluated. I set it to false without taking effect. So the match is replaced.

If you are exporting a CSV or xlsx file, simply change the parameters in the export method to CSV or xlsx. You can also perform a coherent operation to directly save the exported file directly to the server.
Use the Store method:

Excel::create (' User information ', function ($excel) use ($cellData) {            $excel->sheet (' Score ', function ($sheet) use ($ Celldata) {                $sheet->rows ($cellData);            });        }) ->store (' xls ')->export (' xls ');---------------------Kukakawa Source: CSDN Original: 79296470?utm_source=copy copyright notice: This article is the original article of Bo Master, Reprint please attach the blog link!

OK, the export is complete.

2-1 Import:

Import we can directly register with the first step the load method on the façade Excel façade

/**     *     * Excel Import     *    /Public Function Import () {        $filePath = ' storage/exports/'. Iconv (' UTF-8 ', ' GBK ' , ' User information '). XLS ';        Excel::load ($filePath, function ($reader) {            $data = $reader->all ();            DD ($data);        });    }

OK, the import is complete.

Laravel Export Plugin

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.