Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel ExcelʵÏÖExcelµ¼ÈëºÍµ¼³ö£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel excelʵÏÖexcelµ¼ÈëºÍµ¼³ö£¿
ÔÚ¿ª·¢ÖУ¬Ðí¶àʱ¼äÐèҪʹÓõ½Excel±í¸ñ¾ÙÐÐÊý¾Ý´¦Àí£¬ÀýÈçÊý¾Ýµ¼Èë¡¢µ¼³öµÈ²Ù×÷¡£Laravel ExcelÊÇÒ»¸ö¼òÆÓ¶øÇ¿Ê¢µÄLaravelÀ©Õ¹£¬ËüÔÊÐíÎÒÃÇÇáËɵش¦ÀíExcelÎļþ£¬°üÀ¨ExcelÎļþµÄ¶ÁÈ¡¡¢Ð´ÈëºÍµ¼³öµÈ²Ù×÷¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃLaravel ExcelʵÏÖExcelµ¼ÈëºÍµ¼³ö¡£
×°ÖÃLaravel Excel
ÔÚLaravelÏîÄ¿ÖÐʹÓÃLaravel ExcelºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚcomposer.jsonÎļþÖмÓÈëlaravel-excel°üµÄÒÀÀµ¼´¿É¡£ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁ
composer require maatwebsite/excel
µÇ¼ºó¸´ÖÆ
ÉèÖÃLaravel Excel
×°ÖÃLaravel Excelºó£¬ÎÒÃÇÐèÒª¾ÙÐÐһЩÉèÖòŻªÊ¹ÓÃËü¡£Ê×ÏÈ£¬ÔÚconfig/app.phpÎļþÖмÓÈëÒÔÏ´úÂ룺
'providers' => [ // ... MaatwebsiteExcelExcelServiceProvider::class, ], 'aliases' => [ // ... 'Excel' => MaatwebsiteExcelFacadesExcel::class, ],
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇÐèÒªÐû²¼Laravel ExcelµÄÉèÖÃÎļþ£¬Ö´ÐÐÒÔÏÂÏÂÁ
php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider" --tag=config
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚconfigĿ¼ÏÂÌìÉúÒ»¸öexcel.phpÎļþ£¬ÎÒÃÇ¿ÉÒÔÔÚ¸ÃÎļþÖоÙÐÐһЩÉèÖã¬Èçµ¼³öÎļþµÄÃûÌá¢Ä¬Èϵ¼³öÎļþÃûµÈ¡£
ExcelÎļþµ¼³ö
½ÓÏÂÀ´£¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃLaravel ExcelʵÏÖExcelÎļþµÄµ¼³ö¡£
3.1 ½¨Éèµ¼³öÀà
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öµ¼³öÀ࣬ÓÃÓÚ½ç˵µ¼³öÊý¾ÝµÄÃûÌúÍÄÚÈÝ¡£ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁ
php artisan make:export UsersExport --model=User
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚapp/ExportsĿ¼ÏÂÌìÉúÒ»¸öUsersExportÀ࣬ÔÚ¸ÃÀàÖпÉÒÔ½ç˵µ¼³öµÄExcelÎļþµÄÊý¾ÝÃûÌúÍÄÚÈÝ¡£ÀýÈ磬ÈôÊÇÎÒÃÇÒªµ¼³öÓû§±íµÄÊý¾Ý£¬¿ÉÒÔÔÚ¸ÃÀàÖнç˵ÈçÏÂÄÚÈÝ£º
namespace AppExports; use AppModelsUser; use MaatwebsiteExcelConcernsFromCollection; class UsersExport implements FromCollection { public function collection() { return User::all(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýFromCollection½Ó¿ÚÖ¸¶¨Á˵¼³öµÄÊý¾ÝȪԴÊÇUserÄ£×ÓÖеÄËùÓÐÓû§Êý¾Ý¡£
³ýÁËFromCollection½Ó¿Ú£¬Laravel Excel»¹ÌṩÁËÆäËûһЩ½Ó¿Ú£¬ÀýÈçFromQueryºÍFromViewµÈ£¬¿ÉÒÔƾ֤ÏÖʵÐèҪѡÓÃÏìÓ¦µÄ½Ó¿Ú¡£
3.2 ʹÓõ¼³öÀർ³öExcelÎļþ
½ç˵ºÃµ¼³öÀàºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂëÀ´Å²Óõ¼³öÀ࣬½«Êý¾Ýµ¼³öµ½ExcelÎļþÖУº
use AppExportsUsersExport; use MaatwebsiteExcelFacadesExcel; public function export() { return Excel::download(new UsersExport, 'users.xlsx'); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃExcel::download()ÒªÁìÀ´Êä³öExcelÎļþ£¬ÆäÖеÚÒ»¸ö²ÎÊýΪ¸Õ¸Õ½¨ÉèµÄµ¼³öÀ๤¾ß£¬µÚ¶þ¸ö²ÎÊýÊÇExcelÎļþµÄÃû³Æ¡£
ExcelÎļþµ¼Èë
½ÓÏÂÀ´£¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃLaravel ExcelʵÏÖExcelÎļþµÄµ¼Èë¡£
4.1 ½¨Éèµ¼ÈëÀà
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öµ¼ÈëÀ࣬ÓÃÓÚ½ç˵µ¼ÈëÊý¾ÝµÄÃûÌúÍÄÚÈÝ¡£ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁ
php artisan make:import UsersImport --model=User
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚapp/ImportsĿ¼ÏÂÌìÉúÒ»¸öUsersImportÀ࣬ÔÚ¸ÃÀàÖпÉÒÔ½ç˵µ¼ÈëExcelÎļþµÄÊý¾ÝÃûÌúÍÄÚÈÝ¡£ÀýÈ磬ÈôÊÇÎÒÃÇÒªµ¼ÈëÓû§±íµÄÊý¾Ý£¬¿ÉÒÔÔÚ¸ÃÀàÖнç˵ÈçÏÂÄÚÈÝ£º
namespace AppImports; use AppModelsUser; use MaatwebsiteExcelConcernsToModel; use MaatwebsiteExcelConcernsWithHeadingRow; class UsersImport implements ToModel, WithHeadingRow { public function model(array $row) { return new User([ 'name' => $row['name'], 'email' => $row['email'], 'password' => Hash::make($row['password']), ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýToModel½Ó¿Ú½«ExcelÎļþÖеÄÿһÐÐÊý¾ÝÓ³Éäµ½UserÄ£×ÓµÄÊôÐÔÉÏ£¬²¢Í¨¹ýWithHeadingRow½Ó¿ÚÖ¸¶¨ExcelÎļþµÄµÚÒ»ÐÐΪ±íÍ·£¨¼´ÊôÐÔÃû£©¡£
³ýÁËToModelºÍWithHeadingRow½Ó¿Ú£¬Laravel Excel»¹ÌṩÁËÆäËûһЩ½Ó¿Ú£¬ÀýÈçToCollectionºÍToModelµÈ£¬¿ÉÒÔƾ֤ÏÖʵÐèҪѡÓÃÏìÓ¦µÄ½Ó¿Ú¡£
4.2 ʹÓõ¼ÈëÀർÈëExcelÎļþ
½ç˵ºÃµ¼ÈëÀàºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂëÀ´Å²Óõ¼ÈëÀ࣬½«Êý¾Ý´ÓExcelÎļþÖе¼Èëµ½Êý¾Ý¿âÖУº
use AppImportsUsersImport; use MaatwebsiteExcelFacadesExcel; public function import() { Excel::import(new UsersImport, request()->file('file')); return redirect()->back(); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃExcel::import()ÒªÁìÀ´µ¼ÈëExcelÎļþ£¬ÆäÖеÚÒ»¸ö²ÎÊýΪ¸Õ¸Õ½¨ÉèµÄµ¼ÈëÀ๤¾ß£¬µÚ¶þ¸ö²ÎÊýΪÉÏ´«µÄExcelÎļþ¡£
½áÓï
ͨ¹ýLaravel Excel£¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖExcelÎļþµÄµ¼ÈëºÍµ¼³ö¹¦Ð§£¬´ó´ó¼ò»¯ÁËÊý¾Ý´¦ÀíµÄÁ÷³Ì¡£ÔÚÏÖʵÏîÄ¿¿ª·¢ÖУ¬Ê¹ÓÃLaravel Excel¿ÉÒÔ´ó´óÌá¸ß¿ª·¢Ð§ÂÊ£¬½µµÍ¿ª·¢±¾Ç®¡£Ï£Íû±¾ÎĶÔÄãÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel ExcelʵÏÖExcelµ¼ÈëºÍµ¼³ö£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡