@@ -28,45 +28,61 @@ $excel->saveTo('path/file.xlsx');
2828// or save file to specified disk
2929$excel->store('disk', 'path/file.xlsx');
3030```
31- Export a Model
31+
32+ ## Export Data
33+
34+ ### Export a Model
35+ Easy and fast export of a model. This way you export only model data without any styling
3236``` php
3337
3438// Create workbook with sheet named 'Users'
3539$excel = \Excel::create('Users');
3640
37- // Write all users to Excel file
38- $sheet->writeModel(Users::class);
41+ // Export all users to Excel file
42+ $sheet->exportModel(Users::class);
43+
44+ $excel->saveTo('path/file.xlsx');
45+ ```
46+ The following code will write the field names and styles (font and borders) to the first row, and then export all the data of the User model
47+ ``` php
48+
49+ // Create workbook with sheet named 'Users'
50+ $excel = \Excel::create('Users');
3951
4052// Write users with field names in the first row
4153$sheet->withHeaders()
4254 ->applyFontStyleBold()
4355 ->applyBorder('thin')
44- ->writeModel (Users::class);
56+ ->exportModel (Users::class);
4557
4658$excel->saveTo('path/file.xlsx');
4759```
4860
49- Export any collections and arrays
61+ ### Export Any Collections and Arrays
5062``` php
5163// Create workbook with sheet named 'Users'
5264$excel = \Excel::create('Users');
5365
5466$sheet = $excel->getSheet();
55- $users = User::all();
67+ // Get users as collection
68+ $users = User::where('age', '>', 35)->get();
5669$sheet->writeData($users);
5770
5871$sheet = $excel->makeSheet('Records');
72+ // Get collection of records using Query Builder
5973$records = \DB::table('users')->where('age', '>=', 21)->get(['id', 'name', 'birthday']);
6074$sheet->writeData($records);
6175
6276$sheet = $excel->makeSheet('Collection');
77+ // Make custom collection of arrays
6378$collection = collect([
6479 [ 'id' => 1, 'site' => 'google.com' ],
6580 [ 'id' => 2, 'site.com' => 'youtube.com' ],
6681]);
6782$sheet->writeData($collection);
6883
6984$sheet = $excel->makeSheet('Array');
85+ // Make array and write to sheet
7086$array = [
7187 [ 'id' => 1, 'name' => 'Helen' ],
7288 [ 'id' => 2, 'name' => 'Peter' ],
@@ -82,7 +98,7 @@ $sheet->writeData(function () {
8298
8399```
84100
85- ## Advanced usage for data export
101+ ### Advanced Usage for Data Export
86102
87103See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme
88104
@@ -95,13 +111,14 @@ $sheet->setColOptions('c', ['width' => 12, 'text-align' => 'center']);
95111$sheet->setColWidth('d', 'auto');
96112
97113$title = 'This is demo of avadim/fast-excel-laravel';
98- // begin area for direct access to cells
114+ // Begin area for direct access to cells
99115$area = $sheet->beginArea();
100116$area->setValue('A2:D2', $title)
101117 ->applyFontSize(14)
102118 ->applyFontStyleBold()
103119 ->applyTextCenter();
104-
120+
121+ // Write headers to area
105122$area
106123 ->setValue('a4:a5', '#')
107124 ->setValue('b4:b5', 'Number')
@@ -110,20 +127,33 @@ $area
110127 ->setValue('d5', 'Name')
111128;
112129
130+ // Apply styles to headers
113131$area->withRange('a4:d5')
114132 ->applyBgColor('#ccc')
115133 ->applyFontStyleBold()
116134 ->applyOuterBorder('thin')
117135 ->applyInnerBorder('thick')
118136 ->applyTextCenter();
119-
137+
138+ // Write area to sheet
120139$sheet->writeAreas();
121140
141+ // Write data to sheet
122142$sheet->writeData($data);
143+
144+ // Save XLSX-file
123145$excel->saveTo($testFileName);
124146
125147```
126148
149+ ## Import Data
150+
151+ ### Import a Model
152+ To import models, you can use method ``` importModel() ``` . By default, the first row is considered to contain the names of the fields
153+ ``` txt
154+ importModel(string $modelClass, $address = null, $columns = null)
155+ ```
156+
127157## Do you want to support FastExcelLaravel?
128158
129159if you find this package useful you can support and donate to me for a cup of coffee:
0 commit comments