Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Excel´¦ÀíExcelÎļþ£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel excel´¦ÀíexcelÎļþ£¿
Laravel ExcelÊÇÒ»¸ö¿ªÔ´µÄLaravelÀ©Õ¹°ü£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃǸüÀû±ãµØ´¦ÀíExcelÎļþ¡£ÔÚLaravelÏîÄ¿ÖÐʹÓÃLaravel Excel¿ÉÒÔʹÎÒÃǸü¿ì½Ý¡¢ÎÞаµØ¶ÁÈ¡¡¢Ð´ÈëºÍÐÞ¸ÄExcelÎļþ¡£
±¾ÎĽ«ÏÈÈÝLaravel ExcelµÄ×°ÖúÍʹÓÃÒªÁ죬ÒÔ¼°ÔÚLaravelÏîÄ¿ÖÐÔõÑùʹÓÃLaravel ExcelÀ´´¦ÀíExcelÎļþ¡£
Ò»¡¢Laravel ExcelµÄ×°ÖÃ
ÔÚ¾ÙÐÐLaravel ExcelµÄ×°ÖÃ֮ǰ£¬È·±£ÒѾװÖúÃÁËLaravel¿ò¼Ü¡£
×°ÖÃLaravel Excel
ʹÓÃcomposerÀ´×°ÖÃLaravel Excel£¬ÏÂÁîÈçÏ£º
composer require maatwebsite/excel
µÇ¼ºó¸´ÖÆ
ÉèÖÃ
×°ÖÃÍê³ÉÖ®ºó£¬ÐèÒªÔÚconfig/app.phpÉèÖÃÎļþÖмÓÈëLaravel ExcelµÄServiceProviderºÍFacades¡£
Ç뽫ÒÔÏ´úÂ븴ÖƵ½config/app.phpµÄprovidersÊý×éÖУº
MaatwebsiteExcelExcelServiceProvider::class,
µÇ¼ºó¸´ÖÆ
ͬʱ£¬½«ÒÔÏ´úÂ븴ÖƵ½config/app.phpµÄaliasesÊý×éÖУº
'Excel' => MaatwebsiteExcelFacadesExcel::class,
µÇ¼ºó¸´ÖÆ
ÕâÑù¾ÍÍê³ÉÁËLaravel ExcelµÄ×°ÖúÍÉèÖá£
¶þ¡¢Laravel ExcelµÄʹÓÃ
ÏÂÃ潫ÏÈÈÝLaravel ExcelµÄʹÓÃÒªÁì¡£
¶ÁÈ¡ExcelÎļþ
ʹÓÃLaravel Excel¿ÉÒÔÇáËɵضÁÈ¡ExcelÎļþ£¬¾ÍÏñ¶ÁÈ¡Êý¾Ý¿âÒ»Ñù¡£¿ÉÒÔʹÓÃÒÔÏ´úÂë¶ÁÈ¡ExcelÎļþ£º
use MaatwebsiteExcelFacadesExcel; Excel::load('Îļþ·¾¶', function($reader) { // »ñÈ¡ExcelÎļþÖеÄËùÓÐÊý¾Ý $results = $reader->get(); // ±éÀúÿһÐÐÊý¾Ý foreach ($results as $row) { // ´¦ÀíÿһÐÐÊý¾Ý } });
µÇ¼ºó¸´ÖÆ
ÔÚ¶ÁÈ¡ExcelÎļþÖ®ºó£¬¿ÉÒÔ¶ÔÿһÐÐÊý¾Ý¾ÙÐд¦Àí¡£ÈôÊÇExcelÎļþÖб£´æ¶à¸ö±í¸ñ£¬¿ÉÒÔʹÓÃsheet()ÒªÁìÖ¸¶¨Òª¶ÁÈ¡µÄ±í¸ñ£¬ÈçÏÂËùʾ£º
Excel::selectSheets('±í¸ñÃû³Æ')->load('Îļþ·¾¶', function($reader) { // »ñÈ¡±í¸ñÖеÄËùÓÐÊý¾Ý $results = $reader->get(); // ±éÀúÿһÐÐÊý¾Ý foreach ($results as $row) { // ´¦ÀíÿһÐÐÊý¾Ý } });
µÇ¼ºó¸´ÖÆ
дÈëExcelÎļþ
ʹÓÃLaravel Excel¿ÉÒÔÀû±ãµØ½«Êý¾ÝдÈëExcelÎļþÖУ¬Ö»ÐèÒª½«Êý¾ÝÃûÌû¯ÎªExcelÖ§³ÖµÄÃûÌü´¿É¡£¿ÉÒÔʹÓÃÒÔÏ´úÂ뽫Êý¾ÝдÈëExcelÎļþÖУº
use MaatwebsiteExcelFacadesExcel; Excel::create('ÎļþÃû', function($excel) { // ½¨ÉèÒ»¸öÊÂÇé±í $excel->sheet('Sheet1', function($sheet) { // дÈëÊý¾Ý $sheet->row(1, array( 'ÐÕÃû', 'ÄêËê', 'ÐÔ±ð' )); $sheet->row(2, array( 'СÃ÷', 20, 'ÄÐ' )); $sheet->row(3, array( 'Сºì', 18, 'Å®' )); }); })->store('xls', 'Îļþ·¾¶');
µÇ¼ºó¸´ÖÆ
ÔÚÖ´ÐÐstore()ÒªÁìÖ®ºó£¬ExcelÎļþ¾Í»á±»ÉúÑÄÔÚÖ¸¶¨µÄ·¾¶ÖС£
ÐÞ¸ÄExcelÎļþ
ʹÓÃLaravel ExcelÒ²¿ÉÒÔºÜÀû±ãµØÐÞ¸ÄExcelÎļþÖеÄÊý¾Ý¡£¿ÉÒÔʹÓÃÒÔÏ´úÂëÐÞ¸ÄExcelÎļþÖеÄÊý¾Ý£º
use MaatwebsiteExcelFacadesExcel; Excel::load('Îļþ·¾¶', function($reader) { // »ñÈ¡ExcelÎļþÖеÄËùÓÐÊý¾Ý $results = $reader->get(); // ±éÀúÿһÐÐÊý¾Ý foreach ($results as $row) { // ÅжÏÊÇ·ñÐèÒªÐ޸ĸÃÐÐÊý¾Ý if (...) { // ÐÞ¸ÄÊý¾Ý $row->column1 = 'еÄÖµ'; $row->column2 = 'еÄÖµ'; // ... } } // ½«Ð޸ĺóµÄÊý¾ÝÉúÑĵ½ÔExcelÎļþÖÐ $reader->export('xls'); });
µÇ¼ºó¸´ÖÆ
ÔÚÐÞ¸ÄÊý¾ÝÖ®ºó£¬Ê¹ÓÃexport()ÒªÁ콫Ð޸ĺóµÄÊý¾ÝÉúÑĵ½ÔExcelÎļþÖС£
½áÓï
±¾ÎÄÏÈÈÝÁËÔõÑù×°ÖúÍʹÓÃLaravel ExcelÀ´´¦ÀíExcelÎļþ¡£Í¨¹ýʹÓÃLaravel Excel£¬ÎÒÃÇ¿ÉÒÔ¸üÀû±ãµØ¶ÁÈ¡¡¢Ð´ÈëºÍÐÞ¸ÄExcelÎļþ£¬Ìá¸ß¿ª·¢Ð§ÂÊ£¬ïÔÌ·±ËöµÄExcelÎļþ´¦ÀíÊÂÇé¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Excel´¦ÀíExcelÎļþ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡