Laravel Import and export there are many but you find that when you install excal according to Conposer, you find that he is always bricks prompt you: Installation failed
This is laravel5.4 's genius, let's take a look at it!
1. In the root directory of the Laravel project we will see a Composer.json file:
Open and find "Maatwebsite/excel": "~2.0.0" modified to "~2.1.0" save
2. Perform composer update Maatwebsite/excel
3. Edit laravel-config-app.php
Find an array of provides to add Maatwebsite\excel\excelserviceprovider::class to the array,
In the array of aliases, add ' Excel ' = Maatwebsite\excel\facades\excel::class,
4. Execute after add: PHP artisan vendor:publish
So you can build routing controllers and so on.
Code:
<?phpnamespace app\http\controllers; Useilluminate\http\request; Useapp\http\requests; UseApp\http\controllers\controller; UseExcel;classExcelcontrollerextendscontroller{//Excel file export feature by Laravel College Public functionExport () {$cellData= [ [' Learning number ', ' name ', ' score ', [' 10001 ', ' AAAAA ', ' 99 '], [' 10002 ', ' bbbbb ', ' 92 '], [' 10003 ', ' CCCCC ', ' 95 '], [' 10004 ', ' ddddd ', ' 89 '], [' 10005 ', ' eeeee ', ' 96 '], ]; Excel:: Create (' Student score ',function($excel) Use($cellData){ $excel->sheet (' SheetName ',function($sheet) Use($cellData){ $sheet->rows ($cellData); }); })->export (' xls '); }}
If you need export is divided into multiple workspaces (sheet):
Multiple calls $excel->sheet (..... ) , note sheetname avoid duplication
//Import$filePath= ' storage/exports/'.Iconv(' UTF-8 ', ' GBK ', ' 1234 '). xls; Excel:: Load ($filePath,function($reader) { $data=$reader-All (); DD ($data);}); Supplement:if the project uses time, please revise it.: Root directory->config->app' TimeZone ' = ' PRC ',//' UTC ', for Excel time//PRC for Chinese time does not use UTC because time is inaccurate when using the date functionIf the table header is a Chinese character cause the column data cannot be output please modify: root directory->config->Excel' To_ascii ' =false //ture If the Excel header is a Chinese character, the value will be missing .
PHP laravel Framework Import Export Excel, Phpexcel data export divided into multiple workspaces (sheet)