laravelÔõÑù×öµ¼Èëµ¼³öexcel
laravel ÊÇÒ»¿î³£ÓÃµÄ php ¿ª·¢¿ò¼Ü£¬Ëü¾ßÓпìËÙ¿ª·¢¡¢Ò×ÓÚά»¤µÈÌص㡣ÔÚÏÖʵ¿ª·¢ÖУ¬¾³£Éæ¼°µ½Êý¾ÝµÄµ¼ÈëºÍµ¼³ö£¬¸üÏêϸһЩ£¬¾ÍÊÇ excel ÎļþµÄµ¼ÈëºÍµ¼³ö¡£laravel ÔõÑùʵÏÖÊý¾ÝµÄµ¼ÈëºÍµ¼³öÄØ£¿±¾ÎĽ«ÏÈÈÝÔõÑùͨ¹ý laravel ʵÏÖ excel ÎļþµÄµ¼ÈëºÍµ¼³ö¡£
Ò»¡¢×°ÖÃ Laravel Excel
Laravel Excel ÊÇÒ»¸öÕë¶Ô PHPExcel µÄ·â×°£¬Ëü¿ÉÒÔÇáËɵØʵÏÖ Excel ÎļþµÄµ¼ÈëºÍµ¼³ö¡£Òò´Ë£¬ÔÚʵÏÖµ¼ÈëºÍµ¼³ö֮ǰ£¬ÎÒÃÇÐèÒªÊ×ÏÈ×°Öà Laravel Excel¡£
ͨ¹ý Composer ¾ÙÐÐ×°Öã¬Ö´ÐÐÈçÏÂÏÂÁ
composer require maatwebsite/excel
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÐèÒªÔÚ config/app.php ÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
'providers' => [ // ... MaatwebsiteExcelExcelServiceProvider::class, ], 'aliases' => [ // ... 'Excel' => MaatwebsiteExcelFacadesExcel::class, ]
µÇ¼ºó¸´ÖÆ
¶þ¡¢Excel Îļþµ¼³ö
Ê×ÏÈ£¬ÎÒÃÇÀ´¿´ÔõÑùµ¼³ö Excel Îļþ¡£Laravel Excel ÌṩÁËÒ»ÖÖÀàËÆÓÚ½ç˵Êý¾Ý¿âÊý¾ÝÄ£×ӵķ½·¨À´½ç˵ Excel ÎļþµÄµ¼³ö¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÒ»¸ö Excel Ä£°åÀ´½ç˵µ¼³öµÄ Excel ÎļþÄÚÈÝ£¬È»ºó½«Êý¾ÝÌî³äµ½Ä£°åÖС£ÏÂÃæÊÇÏêϸµÄʵÏÖÒªÁ죺
1.½ç˵ Excel ÎļþÄ£°å
ÔÚÏîÄ¿µÄ¸ùĿ¼ÏÂÖ´ÐÐÈçÏÂÏÂÁ
php artisan make:export UsersExport --model=User
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁÔÚ app/Exports Ŀ¼ÏÂÌìÉúÒ»¸öÃûΪ UsersExport µÄÀà¡£¸ÃÀàʵÏÖÁË FromModel ½Ó¿Ú£¬ÓÃÓÚ½«Êý¾Ý´ÓÄ£×ÓÖе¼³öµ½ Excel ÎļþÖС£
2.Ìî³äÊý¾Ý
½ç˵ Excel ÎļþÄ£°åºó£¬ÎÒÃÇÐèҪͨ¹ý´úÂëÏòÆäÖÐÌî³äÊý¾Ý¡£´úÂëʵÏÖÈçÏ£º
namespace AppHttpControllers; use AppExportsUsersExport; use AppHttpControllersController; use MaatwebsiteExcelFacadesExcel; class UsersController extends Controller { public function export() { return Excel::download(new UsersExport, 'users.xlsx'); } }
µÇ¼ºó¸´ÖÆ
3.ÌìÉú Excel Îļþ
ÔÚÍê³ÉÊý¾ÝÌî³äºó£¬¾Í¿ÉÒÔÌìÉú Excel ÎļþÁË¡£Ö´Ðе¼³ö²Ù×÷£¬´úÂëʵÏÖÈçÏ£º
Excel::download(new UsersExport, 'users.xlsx');
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫»áÌìÉúÒ»¸öÃûΪ users.xlsx µÄ Excel Îļþ¡£
Èý¡¢Excel Îļþµ¼Èë
µ¼Èë Excel ÎļþÓëµ¼³ö Excel ÎļþÀàËÆ£¬Í¬ÑùÐèÒªÓÐÒ»¸öÊý¾ÝÄ£×Ó£¬ÓÃÓÚ½« Excel ÎļþÖеÄÊý¾Ýµ¼Èëµ½Êý¾Ý¿âÖС£ÒÔÏÂÊÇÏêϸµÄʵÏÖÒªÁ죺
1.½ç˵ Excel ÎļþÄ£°å
Ê×ÏÈ£¬ÐèÒª½ç˵һ¸ö ContractsFromView ½Ó¿ÚÀ´ÊµÏÖÊý¾ÝÌî³ä£¬´úÂëʵÏÖÈçÏ£º
namespace AppImports; use IlluminateContractsViewView; use MaatwebsiteExcelConcernsFromView; class UsersImport implements FromView { private $users; public function __construct(array $users) { $this->users = $users; } public function view(): View { return view('exports.users', [ 'users' => $this->users ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚʵÏÖ½Ó¿ÚÖ®ºó£¬ÐèÒªÔÚ resources/views/exports Ŀ¼Ï½ç˵һ¸öÃûΪ users.blade.php µÄÎļþ£¬´úÂëÈçÏ£º
<table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Gender</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{ $user['name'] }}</td> <td>{{ $user['email'] }}</td> <td>{{ $user['gender'] }}</td> </tr> @endforeach </tbody> </table>
µÇ¼ºó¸´ÖÆ
Õâ¸öÄ£°å½«»á°ÑÓû§Êý¾ÝÌî³äµ½Ò»¸ö HTML ±í¸ñÖС£
2.µ¼Èë Excel Îļþ
ÔÚ½ç˵Êý¾ÝÄ£×ÓÖ®ºó£¬¾Í¿ÉÒÔͨ¹ý Laravel Excel µÄExcel::importº¯Êý½«Êý¾Ýµ¼Èëµ½Êý¾Ý¿âÖÐÁË£¬Ïêϸ´úÂëÈçÏ£º
namespace AppHttpControllers; use AppImportsUsersImport; use MaatwebsiteExcelFacadesExcel; class UsersController extends Controller { public function import() { $file = request()->file('file'); $users = Excel::toArray(new UsersImport, $file); // ¿ÉÒÔÔÚ´Ë´¦½« $users ²åÈëÊý¾Ý¿â } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ýÊäÈë¿òÑ¡Ôñ Excel Îļþ£¬È»ºó»ñÈ¡ Excel Îļþ£¬½«Æäת»¯ÎªÊý×éÀàÐÍ¡£¹ØÓÚµ¼ÈëµÄÊý¾Ý£¬ÎÒÃÇ¿ÉÒÔÔÚ // ¿ÉÒÔÔÚ´Ë´¦½« $users ²åÈëÊý¾Ý¿â ´¦½«Æä²åÈëµ½Êý¾Ý¿âÖС£
ËÄ¡¢×ܽá
Laravel Excel ÒѾΪÎÒÃÇÌṩÁËÀàËÆÓÚÊý¾Ý¿â²Ù×÷µÄ·½·¨£¬Ê¹ Excel Îļþµ¼ÈëºÍµ¼³ö±äµÃºÜÊǼòÆÓ¡£Laravel Excel ¿ÉÒÔÓë Eloquent Ä£×ÓÎÞ·ìÐ×÷£¬Í¬Ê±Ò²Ö§³ÖÆäËûÃûÌõÄÎļþ£¬ÀýÈç CSV¡¢PDF ºÍ HTML¡£ÎÒÃÇÖ»ÐèҪƾ֤ÉÏÊöÒªÁì×°Öà Laravel Excel ²¢½ç˵ºÏÊʵÄÄ£×Ó¼´¿ÉÔÚ Laravel ÖÐʵÏÖÊý¾ÝµÄµ¼ÈëºÍµ¼³ö¡£
ÒÔÉϾÍÊÇlaravelÔõÑù×öµ¼Èëµ¼³öexcelµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡