×ðÁú¿­Ê±¹ÙÍøµÇ¼

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µ¼ÈëºÍµ¼³ö£¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ