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

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel ExcelºÍPHPOffice¶ÁдExcelÎļþ£¿

Ëæ×ÅexcelÎļþÔÚÉÌÒµºÍ²ÆÎñÁìÓòÖеÄÆÕ±éʹÓ㬽«excelÎļþÕûºÏµ½webÓ¦ÓóÌÐòÖÐÒѾ­³ÉΪ¿ª·¢Ö°Ô±ËùÐèµÄÊÖÒÕÖ®Ò» ¡£ÓÉÓÚlaravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ò¼Ü£¬ÆäÉú̬ϵͳÖÐÓÐÐí¶à°ü¿ÉÒÔ×ÊÖúÎÒÃǶÁдexcelÎļþ ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel excelºÍphpoffice¿âÔÚlaravelÓ¦ÓóÌÐòÖжÁдexcelÎļþ ¡£

ʲôÊÇLaravel Excel£¿

Laravel ExcelÊÇÒ»¸öΪLaravel¿ò¼Ü¿ª·¢µÄÇ¿Ê¢Excel´¦Àí¹¤¾ß ¡£¸Ã¹¤¾ßÌṩÁËÒ×ÓÚʹÓõÄAPI£¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵص¼ÈëºÍµ¼³öExcelÎļþ ¡£¸Ã¹¤¾ßµÄÒ»¸öÖ÷ÒªÓŵãÊÇËüÔÊÐíÖ±½Ó²Ù×÷ExcelÎļþ¶øÎÞÐèÒÀÀµÓÚExcelÈí¼þ ¡£

×°ÖÃLaravel Excel¿ÉÒÔͨ¹ýComposerÍê³É ¡£ÔÚÖÕ¶ËÖнøÈëLaravelÏîÄ¿µÄĿ¼£¬²¢ÔËÐÐÏÂÃæµÄComposerÏÂÁ

composer require maatwebsite/excel

µÇ¼ºó¸´ÖÆ

¸ÃÏÂÁ´ÓPackagistÏÂÔØ×îа汾µÄLaravel Excel²¢×Ô¶¯Íê³É×°ÖÃÀú³Ì ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

ÔõÑùʹÓÃLaravel Excelµ¼³öExcelÎļþ£¿

Laravel ExcelÌṩÁËÒ»¸öExcelÀ࣬ËüÊÇÒ»¸öÊðÀíÀ࣬ͨ¹ýËü¿ÉÒÔ½¨ÉèеÄExcelÎļþ»ò·­¿ª²¢±à¼­ÏÖÓеÄExcelÎļþ ¡£ÎÒÃÇ¿ÉÒÔʹÓøÃÀàÔÚLaravelÓ¦ÓóÌÐòÖе¼³öExcelÎļþ ¡£

ÔÚÏîÄ¿Öн¨ÉèÒ»¸öеÄExcelÎļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravel ExcelÌṩµÄÒÔÏÂÄ£°å£º

<?php

namespace AppExports;

use MaatwebsiteExcelConcernsFromCollection;
use MaatwebsiteExcelConcernsWithHeadings;

class ExampleExport implements FromCollection, WithHeadings
{
    public function headings(): array
    {
        return [
            'ID',
            'Name',
            'Email',
            'Phone',
        ];
    }

    /**
    * @return IlluminateSupportCollection
    */
    public function collection()
    {
        return User::select('id', 'name', 'email', 'phone')->get();
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪExampleExportµÄÀà ¡£¸ÃÀàʹÓÃFromCollectionºÍWithHeadings½Ó¿Ú£¬ÕâЩ½Ó¿ÚÌṩÁËÌìÉúExcelÎļþËùÐèµÄÒªÁì ¡£

ÔÚheadings()ÒªÁìÖУ¬ÎÒÃǽç˵ÁËExcelÎļþµÄÎÊÌ⣬ÕâЩÎÊÌâÁÐÔÚExcelÊÂÇé±íµÄµÚÒ»ÐÐÖÐ ¡£ÔÚcollect()ÒªÁìÖУ¬ÎÒÃÇ´ÓÊý¾Ý¿âÖмìË÷Óû§¼Í¼²¢½«Æä·µ»¹¸øŲÓøÃÀàµÄ¿ØÖÆÆ÷ ¡£

Òªµ¼³öExcelÎļþ£¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÒªÁìÖÐʵÀý»¯ExampleExport£¬ÈçÏÂËùʾ£º

public function export()
{
    return Excel::download(new ExampleExport, 'users.xlsx');
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃLaravel ExcelÌṩµÄdownload()ÒªÁì´ÓExampleExportµ¼³öÒ»¸öExcelÎļþ ¡£¸ÃÒªÁìÐèÒªÁ½¸ö²ÎÊý£ºµÚÒ»¸ö²ÎÊýÊÇExampleExportʵÀý£¬µÚ¶þ¸ö²ÎÊýÊÇExcelÎļþÃû ¡£

ÔÚä¯ÀÀÆ÷Öлá¼ûµ¼³öÒªÁìµÄURLʱ£¬ÏµÍ³½«»áÌáÐÑÄúÏÂÔØusers.xlsxÎļþ ¡£

ÔõÑùʹÓÃLaravel Excelµ¼ÈëExcelÎļþ£¿

¶ÁÈ¡ExcelÎļþ²¢½«ÆäÊý¾Ýµ¼ÈëÊý¾Ý¿âʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravel ExcelÌṩµÄimport()ÒªÁì ¡£¸ÃÒªÁì½ÓÊÜÈý¸ö²ÎÊý£ºÎļþ¹¤¾ß¡¢Ê¹Ãü»ØЭµ÷ÊÂÇé±íÃû³Æ ¡£

ΪÁËÑÝʾÔõÑùµ¼ÈëExcelÎļþ£¬ÎÒÃǽ«½¨ÉèÒ»¸öÃûΪUserImportµÄÀ࣬ÈçÏÂËùʾ£º

<?php

namespace AppImports;

use AppUser;
use MaatwebsiteExcelConcernsToModel;

class UserImport implements ToModel
{
    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
            'email' => $row[1],
            'phone' => $row[2],
        ]);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃToModel½Ó¿ÚʵÏÖÁËUserImportÀà ¡£ToModel½Ó¿ÚÌṩÁËÒ»¸ö±ØÐèµÄmodel()ÒªÁ죬¸ÃÒªÁ콫ÿһÐÐExcelÊý¾Ýת»»ÎªÄ£×Ó¹¤¾ß ¡£ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÆÊÎöÁËExcelÎļþÖеÄÇ°Èý¸öÁв¢½«ÆäÓÃÓÚ½¨ÉèеÄÓû§¼Í¼ ¡£

ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃimport()ÒªÏòµ¼ÈëExcelÎļþ£¬ÈçÏÂËùʾ£º

public function import(Request $request)
{
    $file = $request->file('file');
    Excel::import(new UserImport, $file);
    return redirect()->back()->with('success', 'Excel file imported successfully.');
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇ»ñÈ¡ÉÏ´«µÄExcelÎļþ¹¤¾ß²¢Í¨¹ýLaravel ExcelµÄimport()ÒªÁ콫Æäµ¼Èëµ½UserImportÀàÖÐ ¡£ÈôÊǵ¼ÈëÀֳɣ¬ÏµÍ³½«ÏòÓû§·¢ËÍ´øÓС°ExcelÎļþÀֳɵ¼È롱µÄÀÖ³ÉÐÂÎŵÄÖض¨ÏòÏìÓ¦ ¡£

ÔõÑùʹÓÃPHPOffice¿â¶ÁдExcelÎļþ£¿

PHPOfficeÊÇÒ»¸öPHP¿â£¬ÓÃÓÚ¶Áд²î±ðÀàÐ͵İ칫ÊÒÎļþ£¬ÈçExcel¡¢WordºÍPowerPointµÈ ¡£PHPOfficeÓëLaravel Excel²î±ð£¬Ëü²»ÊÇΪÌض¨µÄ¿ò¼Ü¿ª·¢µÄ£¬¿ÉÒÔÓÃÓÚÈκÎPHPÓ¦ÓóÌÐòÖÐ ¡£

ÔÚ×°ÖÃPHPOffice֮ǰ£¬ÄúÐèҪȷ±£ÒÑ×°ÖÃPHP ZipÀ©Õ¹ºÍPHP XMLÀ©Õ¹ ¡£

ʹÓÃComposer×°ÖÃPHPOfficeµÄSpreadsheet¿â£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ

composer require phpoffice/phpspreadsheet

µÇ¼ºó¸´ÖÆ

Òª½¨ÉèÒ»¸öеÄExcelÎļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

<?php

use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');
$sheet->setCellValue('B1', 'This is PHPOffice.');

$writer = new Xlsx($spreadsheet);
$writer->save('hello.xlsx');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÒ»¸öеĵç×Ó±í¸ñ£¬²¢ÔÚÆäµÚÒ»ÐеÄÇ°Á½¸öÁÐÖÐÌí¼ÓÁË¡°Hello World!¡±ºÍ¡°This is PHPOffice.¡±µÄµ¥Î»¸ñ ¡£È»ºó£¬ÎÒÃǽ«µç×Ó±í¸ñÉúÑĵ½hello.xlsxÎļþÖÐ ¡£

Òª·­¿ªºÍ±à¼­ÏÖÓеÄExcelÎļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂ룺

<?php

use PhpOfficePhpSpreadsheetIOFactory;

$spreadsheet = IOFactory::load('hello.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('C1', 'This cell has been added.');

$writer = new Xlsx($spreadsheet);
$writer->save('hello.xlsx');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃIOFactoryÀà´Ó´ÅÅ̼ÓÔØÏÖÓеÄExcelÎļþ ¡£È»ºó£¬ÎÒÃÇ·­¿ªÁ˸ÃÎļþµÄÔ˶¯ÊÂÇé±í£¬²¢ÏòÆäÌí¼ÓÁËÒ»¸öеĵ¥Î»¸ñ ¡£×îºó£¬ÎÒÃǽ«¸üÐÂÉúÑĵ½Ô­Ê¼ÎļþÖÐ ¡£

½áÂÛ

ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃLaravel ExcelºÍPHPOffice¿âÔÚLaravelÓ¦ÓóÌÐòÖжÁдExcelÎļþ ¡£ÎÒÃÇѧϰÁËÔõÑùʹÓÃLaravel Excelµ¼³öºÍµ¼ÈëExcelÎļþ£¬ÒÔ¼°ÔõÑùʹÓÃPHPOffice¿â½¨Éè¡¢·­¿ªºÍ±à¼­ÏÖÓеÄExcelÎļþ ¡£ÕâЩÊÖÒÕÓ¦¸ÃʹÄúÄܹ»¸üÇáËɵØÕûºÏExcelÎļþµ½ÄúµÄLaravelÓ¦ÓóÌÐòÖÐ ¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel ExcelºÍPHPOffice¶ÁдExcelÎļþ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ