post-image

Import và export file excel, csv với Laravel-Excel

Tổng quan

Bài viết này mình sẽ tổng quan ngắn gọn về package Laravel-Excel. Có lẽ không cần phải giải thích nhiều về mục đích của nó, tiêu đề nói tất cả mọi thứ. Về cơ bản, Laravel Excel mang trong mình sức mạnh của PHPExcel, nó bao gồm các tính năng như: importing Excel, CSV to collection, exporting models, array’s hoặc views to Excel, importing nhiều file, v.v..

laravel-excel.jpg

Một số tính năng vượt trội của Laravel Excel

  • Import file excel, csv into Laravel Collections
  • Export Blade views to Excel and CSV with CSS styling
  • Import nhiều file
  • Hỗ trợ caching
  • Hỗ trợ chunk and queues importer
  • Sửa file Excel, csv
  • Nhiều thiết lập cấu hình tùy chọn trong file config
  • Và còn rất nhiều tính năng khác

Sử dụng Laravel Excel

1 – Cài đặt

  • Cài đặt với composer

composer require maatwebsite/excel

  • Sau khi cài đặt xong bạn mở file file config/app.php và thêm đoạn code như dưới.
'providers' => [ .... Maatwebsite\Excel\ExcelServiceProvider::class, ], 'aliases' => [ .... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ],
Code language: PHP (php)
  • Public các thiết lập cấu hình:

php artisan vendor:publish

Thiết lập trên sẽ thêm file excel.php vào thư mực config.

2 – Import

Laravel Excel có thể import nhiều file, file xls, xlsx, CSV files, worksheets thành Laravel collections.

  • Importing một file
Excel::load('file.xls', function($reader) { // reader methods });
Code language: PHP (php)
  • Import một thư mục
Excel::batch('folder', function($rows, $file) { // Explain the reader how it should interpret each row, // for every file inside the batch $rows->each(function($row) { // Example: dump the firstname dd($row->firstname); }); });
Code language: PHP (php)
  • Import nhiều files
$files = [ 'file1.xls', 'file2.xls' ]; Excel::batch($files, function($rows, $file) { });
Code language: PHP (php)
  • Edit files
Excel::load('file.csv', function($file) { // modify })->export('csv');
Code language: PHP (php)
  • Convert file
Excel::load('file.csv', function($file) { // modify stuff })->convert('xls');
Code language: PHP (php)

3 – Export

Laravel Excel có thể tạo file Excel hoặc CSV từ Eloquent models and PHP array.

  • Export to Excel5 (xls)
Excel::create('Filename', function($excel) { })->export('xls'); // or ->download('xls');
Code language: PHP (php)
  • Export to Excel2007 (xlsx)
->export('xlsx'); // or ->download('xlsx');
Code language: PHP (php)
  • Export to CSV
->export('csv'); // or ->download('csv');
Code language: PHP (php)
  • Store on server
Excel::create('fileName', function($excel) { // Set sheets })->store('xls');
Code language: PHP (php)
  • Creating a sheet
Excel::create('Filename', function($excel) { $excel->sheet('Sheetname', function($sheet) { // Sheet manipulation }); })->export('xls');
Code language: PHP (php)

4 – Blade to Excel

Bạn có thể sử dụng Laravel’s Blade để export file excel, chia sẽ một view, tải một view tới sheet hay tạo một bảng html bên trong view.

  • Load một view tới một sheet bạn sữ dụng ->loadView().
Excel::create('New file', function($excel) { $excel->sheet('New sheet', function($sheet) { $sheet->loadView('folder.view'); }); });
Code language: PHP (php)
  • Sử dụng các view khác nhau cho các sheet khác nhau
Excel::create('New file', function($excel) { $excel->sheet('First sheet', function($sheet) { $sheet->loadView('view_first'); }); $excel->sheet('Second sheet', function($sheet) { $sheet->loadView('view_second'); }); });
Code language: PHP (php)
  • Chia sẻ view cho tất cả các sheet
Excel::shareView('folder.view')->create();
Code language: PHP (php)
  • Truyền biến vào view
$sheet->loadView('view', ['key' => 'value']);
Code language: PHP (php)

hoặc

// Using normal with() $sheet->loadView('view') ->with('key', 'value'); // using dynamic with() $sheet->loadView('view') ->withKey('value');
Code language: PHP (php)

Trên đây là một số tính năng cơ bản của Larave Excel. Hy vọng bài viết này sẽ giúp các bạn nắm bắt được một phần nào về Laravel Excel.

Các bạn có thể tham khảo các bài viết hay về Laravel tại đây.


Hãy tham gia nhóm Học lập trình để thảo luận thêm về các vấn đề cùng quan tâm.

Tham khảo: Viblo

Leave a Reply

Your email address will not be published.