laravel excelÉèÖÃÍ·
ÔÚʹÓÃlaravel excelʱ£¬ÉèÖñíÍ·ÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄʹÃü¡£±íͷͨ³£ÓÃÓÚÖ¸¶¨°üÀ¨ÔÚexcelÎļþÖеÄÁеÄÃû³Æ£¬²¢ÇÒʹµÃÊý¾Ý¼¯Ô½·¢Ò×ÓÚÃ÷È·ºÍÆÊÎö¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«»áÏÈÈÝÔõÑùÉèÖÃÍ·¡£
Laravel ExcelÊÇÒ»¸ö¿ªÔ´µÄ¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´¶ÁÈ¡¡¢²Ù×÷ºÍµ¼³öExcelÎļþ£¬ËüÌṩÁËÒ»ÖÖ¼òÆÓ¡¢ÓÅÑŵÄÒªÁìÀ´´¦Öóͷ£ExcelÎļþ£¬²¢ÇÒʹµÃExcelÎļþµÄÌìÉúÀú³ÌÔ½·¢ÇáËÉ¡£
Laravel Excelͨ¹ýÉèÖñíÍ·À´¾ÙÐÐÊý¾Ýµ¼³ö¡£ÔÚÎÒÃÇÌìÉúExcelÎļþ֮ǰ£¬ÎÒÃÇÐèÒª½ç˵ExcelÎļþµÄ±íÍ·£¬Õâ¸öÀú³Ìͨ³£±»³ÆΪ½ç˵ÎÊÌâÐС£
ÏÂÃæ¾ÍÊÇÉèÖñíÍ·µÄһЩҪÁ죬ÈÃÎÒÃÇ×îÏÈ°É£¡
ʹÓÃwithHeadings()ÒªÁì
withHeadings()ÒªÁìÊÇLaravel ExcelÌṩµÄÒ»¸öÄÚÖÃÒªÁ죬ËüÔÊÐíÎÒÃǽç˵±íÍ·¡£Õâ¸öÒªÁì½ÓÊÜÒ»¸öÊý×é×÷Ϊ²ÎÊý£¬Êý×éµÄÿ¸öÔªËض¼ÊÇÒ»¸ö±íÍ·µ¥Î»¸ñµÄÖµ¡£
ʾÀý´úÂ룺
use MaatwebsiteExcelFacadesExcel; Excel::create('my_excel_file', function($excel) { $excel->sheet('Sheet 1', function($sheet) { $header = ['Name', 'Email', 'Phone']; $data = [ ['John Doe', 'johndoe@gmail.com', '123-456-7890'], ['Jane Doe', 'janedoe@gmail.com', '123-456-7890'], ['Bob Smith', 'bobsmith@gmail.com', '123-456-7890'] ]; $sheet->with($data)->withHeadings($header); }); })->download('xlsx');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö°üÀ¨¡°Name¡±¡¢¡°Email¡±ºÍ¡°Phone¡±µÄ±íÍ·Êý×飬²¢½«Æäת´ï¸øÁËwithHeadings()ÒªÁì¡£
ʹÓÃfromArray()ÒªÁì
fromArray()ÒªÁìÊÇÁíÒ»¸öÉèÖñíÍ·µÄÒªÁì¡£ÓëwithHeadings()ÒªÁì²î±ð£¬ÎÒÃDz»ÐèҪʹÓÃfromArray()ÒªÁìÀ´»®·ÖÖ¸¶¨±íÍ·ºÍÊý¾Ý¡£
ʾÀý´úÂ룺
use MaatwebsiteExcelFacadesExcel; Excel::create('my_excel_file', function($excel) { $excel->sheet('Sheet 1', function($sheet) { $data = [ ['Name', 'Email', 'Phone'], ['John Doe', 'johndoe@gmail.com', '123-456-7890'], ['Jane Doe', 'janedoe@gmail.com', '123-456-7890'], ['Bob Smith', 'bobsmith@gmail.com', '123-456-7890'] ]; $sheet->fromArray($data); }); })->download('xlsx');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ«Õû¸ö±í¸ñ½ç˵Ϊһ¸öÊý×飬°üÀ¨±íÍ·ºÍÊý¾Ý£¬²¢½«Æäת´ï¸øÁËfromArray()ÒªÁì¡£
´ÓÊý¾Ý¿âÖÐÌáÈ¡Êý¾Ý²¢ÉèÖñíÍ·
ÎÒÃÇ¿ÉÒÔʹÓÃLaravel ModelÀ´´ÓÊý¾Ý¿âÖÐÌáÈ¡Êý¾Ý¡£ÎÒÃÇ¿ÉÒÔʹÓÃwithHeadings()ÒªÁìÀ´ÉèÖñíÍ·¡£
ʾÀý´úÂ룺
use AppUser; use MaatwebsiteExcelFacadesExcel; Excel::create('my_excel_file', function($excel) { $excel->sheet('Sheet 1', function($sheet) { $users = User::select('id', 'name', 'email')->get()->toArray(); $sheet->with($users)->withHeadings(['ID', 'Name', 'Email']); }); })->download('xlsx');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃEloquentÀ´»ñÈ¡Óû§Êý¾Ý²¢½«Æäת»»ÎªÊý×é¡£½ÓÏÂÀ´£¬ÎÒÃÇʹÓÃwithHeadings()ÒªÁìÀ´ÉèÖñíÍ·¡£
½áÂÛ
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÈýÖÖÉèÖÃLaravel Excel±íÍ·µÄÒªÁì¡£ÎÞÂÛÄãÊÇʹÓÃwithHeadings()ÒªÁì¡¢fromArray()ÒªÁìÕվɴÓÊý¾Ý¿âÖÐÌáÈ¡Êý¾Ý²¢ÉèÖñíÍ·£¬Laravel ExcelÈÃÌìÉúExcelÎļþ±äµÃºÜÊǼòÆÓ¡£Ï£Íû±¾ÎÄ¿ÉÒÔ×ÊÖúÄú¸üºÃµØʹÓÃLaravel Excel¡£
ÒÔÉϾÍÊÇlaravel excelÉèÖÃÍ·µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡