|
1 | | -# fast-excel-laravel |
2 | | -Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel |
| 1 | +# FastExcelLaravel |
| 2 | +Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel |
| 3 | +(wrapper for [FastExcelWriter](https://packagist.org/packages/avadim/fast-excel-writer)) |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +This library is a wrapper for avadim/fast-excel-writer, so it's also lightweight, fast, and requires a minimum of memory. |
| 8 | +Using this library, you can export arrays, collections and models to a XLSX-file from your Laravel application. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Install via composer: |
| 13 | + |
| 14 | +``` |
| 15 | +composer require avadim/fast-excel-laravel |
| 16 | +``` |
| 17 | +And then you can use facade ```Excel``` |
| 18 | + |
| 19 | +```php |
| 20 | +// Create workbook |
| 21 | +$excel = \Excel::create(); |
| 22 | +// do something... |
| 23 | +// and save XLSX-file to default storage |
| 24 | +$excel->saveTo('path/file.xlsx'); |
| 25 | + |
| 26 | +// or save file to specified disk |
| 27 | +$excel->store('disk', 'path/file.xlsx'); |
| 28 | +``` |
| 29 | +Export a Model |
| 30 | +```php |
| 31 | + |
| 32 | +// Create workbook with sheet named 'Users' |
| 33 | +$excel = \Excel::create('Users'); |
| 34 | + |
| 35 | +// Write all users to Excel file |
| 36 | +$sheet->writeModel(Users::class); |
| 37 | + |
| 38 | +// Write users with field names in the first row |
| 39 | +$sheet->withHeaders() |
| 40 | + ->applyFontStyleBold() |
| 41 | + ->applyBorder('thin') |
| 42 | + ->writeModel(Users::class); |
| 43 | + |
| 44 | +$excel->saveTo('path/file.xlsx'); |
| 45 | +``` |
| 46 | + |
| 47 | +Export any collections and arrays |
| 48 | +```php |
| 49 | +// Create workbook with sheet named 'Users' |
| 50 | +$excel = \Excel::create('Users'); |
| 51 | + |
| 52 | +$sheet = $excel->getSheet(); |
| 53 | +$users = User::all(); |
| 54 | +$sheet->writeData($users); |
| 55 | + |
| 56 | +$sheet = $excel->makeSheet('Records'); |
| 57 | +$records = \DB::table('users')->where('age', '>=', 21)->get(['id', 'name', 'birthday']); |
| 58 | +$sheet->writeData($records); |
| 59 | + |
| 60 | +$sheet = $excel->makeSheet('Collection'); |
| 61 | +$collection = collect([ |
| 62 | + [ 'id' => 1, 'site' => 'google.com' ], |
| 63 | + [ 'id' => 2, 'site.com' => 'youtube.com' ], |
| 64 | +]); |
| 65 | +$sheet->writeData($collection); |
| 66 | + |
| 67 | +$sheet = $excel->makeSheet('Array'); |
| 68 | +$array = [ |
| 69 | + [ 'id' => 1, 'name' => 'Helen' ], |
| 70 | + [ 'id' => 2, 'name' => 'Peter' ], |
| 71 | +]; |
| 72 | +$sheet->writeData($array); |
| 73 | + |
| 74 | +$sheet = $excel->makeSheet('Callback'); |
| 75 | +$sheet->writeData(function () { |
| 76 | + foreach (User::cursor() as $user) { |
| 77 | + yield $user; |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +``` |
| 82 | + |
| 83 | +## Advanced usage |
| 84 | + |
| 85 | +See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme |
| 86 | + |
| 87 | +```php |
| 88 | +$excel = \Excel::create('Users'); |
| 89 | +$sheet = $excel->getSheet(); |
| 90 | + |
| 91 | +$sheet->setColWidth('B', 12); |
| 92 | +$sheet->setColOptions('c', ['width' => 12, 'text-align' => 'center']); |
| 93 | +$sheet->setColWidth('d', 'auto'); |
| 94 | + |
| 95 | +$title = 'This is demo of avadim/fast-excel-laravel'; |
| 96 | +// begin area for direct access to cells |
| 97 | +$area = $sheet->beginArea(); |
| 98 | +$area->setValue('A2:D2', $title) |
| 99 | + ->applyFontSize(14) |
| 100 | + ->applyFontStyleBold() |
| 101 | + ->applyTextCenter(); |
| 102 | + |
| 103 | +$area |
| 104 | + ->setValue('a4:a5', '#') |
| 105 | + ->setValue('b4:b5', 'Number') |
| 106 | + ->setValue('c4:d4', 'Movie Character') |
| 107 | + ->setValue('c5', 'Birthday') |
| 108 | + ->setValue('d5', 'Name') |
| 109 | +; |
| 110 | + |
| 111 | +$area->withRange('a4:d5') |
| 112 | + ->applyBgColor('#ccc') |
| 113 | + ->applyFontStyleBold() |
| 114 | + ->applyOuterBorder('thin') |
| 115 | + ->applyInnerBorder('thick') |
| 116 | + ->applyTextCenter(); |
| 117 | + |
| 118 | +$sheet->writeAreas(); |
| 119 | + |
| 120 | +$sheet->writeData($data); |
| 121 | +$excel->saveTo($testFileName); |
| 122 | + |
| 123 | +``` |
| 124 | + |
| 125 | +## Do you want to support FastExcelLaravel? |
| 126 | + |
| 127 | +if you find this package useful you can support and donate to me for a cup of coffee: |
| 128 | + |
| 129 | +* USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7 |
| 130 | +* USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b |
| 131 | + |
| 132 | +Or just give me star on GitHub :) |
0 commit comments