12/08/2018, 14:15

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

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 ...

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' => [
	....
	MaatwebsiteExcelExcelServiceProvider::class,
],

'aliases' => [
	....
	'Excel' => MaatwebsiteExcelFacadesExcel::class,
],

  • 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
});
  • 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);

    });

});
  • Import nhiều files
$files = [
    'file1.xls',
    'file2.xls'
];

Excel::batch($files, function($rows, $file) {

});

  • Edit files
Excel::load('file.csv', function($file) {
    // modify

})->export('csv');

  • Convert file
Excel::load('file.csv', function($file) {
    // modify stuff

})->convert('xls');

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');

  • Export to Excel2007 (xlsx)
->export('xlsx');

// or
->download('xlsx');
  • Export to CSV
->export('csv');

// or
->download('csv');
  • Store on server
Excel::create('fileName', function($excel) {

    // Set sheets

})->store('xls');
  • Creating a sheet
Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        // Sheet manipulation

    });

})->export('xls');

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');

    });

});
  • 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');
    });

});
  • Chia sẻ view cho tất cả các sheet
Excel::shareView('folder.view')->create();
  • Truyền biến vào view
$sheet->loadView('view', ['key' => 'value']);

hoặc

// Using normal with()
$sheet->loadView('view')
    ->with('key', 'value');

// using dynamic with()
$sheet->loadView('view')
    ->withKey('value');

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.

Tham khảo:

  • http://www.maatwebsite.nl/laravel-excel/docs
0