[Laravel] Nhập và xuất file excel, csv với Laravel-Excel

Đăng bởi: Admin | Lượt xem: 2902 | Chuyên mục: Laravel

Bài viết hôm nay mình sẽ hướng dẫn các bạn sử dụng package Laravel-Excel . Về cơ bản, Laravel Excel 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..


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,
],

  • 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:

vncoder logo

Theo dõi VnCoder trên Facebook, để cập nhật những bài viết, tin tức và khoá học mới nhất!



Khóa học liên quan

Khóa học: Laravel

Xây dựng ứng dụng với Laravel và Vuejs
Số bài học:
Lượt xem: 15907
Đăng bởi: Admin
Chuyên mục: Laravel

Học lập trình Laravel
Số bài học:
Lượt xem: 22903
Đăng bởi: Admin
Chuyên mục: Laravel