尊龙凯时人生就是搏

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
sitemap¡¢ÍøÕ¾µØͼ