尊龙凯时人生就是搏

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ