Laravel Excel brings the power of PHPOffice's PHPExcel to Laravel 5 with a touch of the Laravel Magic. It includes features like: importing Excel and CSV to collections, exporting models, array's and views to Excel, importing batches of files and importing a file by a config file.
This project is a very simple demo to show you how to use the laravel excel package quickly.
This project was created by The EST Group and PHPHub.
You can refer to this documentation to know how to run this demo.
- Installation
- Basic Usage
- More Usage
Require this package in your composer.json and update composer. This will download the package and PHPExcel of PHPOffice.
"maatwebsite/excel": "~2.1.0"
After updating composer, add the ServiceProvider to the providers array in config/app.php
'providers' => [
...
Maatwebsite\Excel\ExcelServiceProvider::class,
],
You can use the facade for shorter code. Add this to your aliases:
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
To publish the config settings in Laravel 5 use:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
This will add an excel.php
config file to your config folder.
# $excel_file_path = Your excel file path
$excel_data = Excel::load($excel_file_path, function($reader) {
$excel_data = Excel::load($excel_file_path)->get()->toArray();
echo 'job.xlsx contents is:';
dd($excel_data);
});
// Export Excel
Excel::create($export_file_name, function ($excel) {
$excel->sheet('Sheetname', function ($sheet) {
$sheet->appendRow(['data 1', 'data 2']);
$sheet->appendRow(['data 3', 'data 4']);
$sheet->appendRow(['data 5', 'data 6']);
});
})->download('xls');
// Export Excel and save it to a specified folder
Excel::create($export_file_name, function ($excel) {
$excel->sheet('Sheetname', function ($sheet) {
$sheet->appendRow(['data 1', 'data 2']);
$sheet->appendRow(['data 3', 'data 4']);
$sheet->appendRow(['data 5', 'data 6']);
});
})->store('xls', $object_path);
Then you can get something like this.
- Selecting sheets and columns;
- Format Dates;
- Calculate formulas;
- Caching and Cell caching;
- Chunk importer;
- Converting;
- @Blade to Excel
You can refer to the documentation to learn more about this package.
欢迎关注 LaravelTips
, 一个专注于为 Laravel 开发者服务, 致力于帮助开发者更好的掌握 Laravel 框架, 提升开发效率的微信公众号.