@@ -19,6 +19,7 @@ Using this library, you can export arrays, collections and models to a XLSX-file
1919* You can set the height of the rows and the width of the columns (including auto width calculation)
2020* Import workbooks and worksheets to Eloquent models very quickly and with minimal memory usage
2121* Automatic field detection from imported table headers
22+ * Mapping import/export data
2223
2324## Installation
2425
@@ -34,7 +35,7 @@ And then you can use facade ```Excel```
3435$excel = \Excel::create();
3536
3637// export model...
37- $sheet->-> withHeadings()->exportModel(Users::class);
38+ $sheet->withHeadings()->exportModel(Users::class);
3839
3940// and save XLSX-file to default storage
4041$excel->saveTo('path/file.xlsx');
@@ -46,25 +47,27 @@ $excel->store('disk', 'path/file.xlsx');
4647$excel = \Excel::open(storage_path('path/file.xlsx'));
4748
4849// import records to database
49- $excel->importModel(User::class);
50+ $excel->withHeadings()-> importModel(User::class);
5051```
5152
5253Jump To:
5354* [ Export Data] ( #export-data )
5455 * [ Export a Model] ( #export-a-model )
5556 * [ Export Any Collections and Arrays] ( #export-any-collections-and-arrays )
57+ * [ Mapping Export Data] ( #mapping-export-data )
5658 * [ Advanced Usage for Data Export] ( #advanced-usage-for-data-export )
5759* [ Import Data] ( #import-data )
5860 * [ Import a Model] ( #import-a-model )
5961 * [ Advanced Usage for Data Import] ( #advanced-usage-for-data-import )
62+ * [ Mapping Import Data] ( #mapping-import-data )
6063* [ More Features] ( #more-features )
6164* [ Do you want to support FastExcelLaravel?] ( #do-you-want-to-support-fastexcellaravel )
6265
6366
6467## Export Data
6568
6669### Export a Model
67- Easy and fast export of a model. This way you export only model data without any styling
70+ Easy and fast export of a model. This way you export only model data without headers and without any styling
6871``` php
6972
7073// Create workbook with sheet named 'Users'
@@ -91,6 +94,21 @@ $sheet->withHeadings()
9194$excel->saveTo('path/file.xlsx');
9295```
9396
97+ ### Mapping Export Data
98+
99+ You can map the data that needs to be added as row
100+
101+ ``` php
102+ $sheet = $excel->getSheet();
103+ $sheet->mapping(function($model) {
104+ return [
105+ 'id' => $model->id, 'date' => $model->created_at, 'name' => $model->first_name . $model->last_name,
106+ ];
107+ })->exportModel(User::class);
108+ $excel->save($testFileName);
109+
110+ ```
111+
94112### Export Any Collections and Arrays
95113``` php
96114// Create workbook with sheet named 'Users'
@@ -99,6 +117,11 @@ $excel = \Excel::create('Users');
99117$sheet = $excel->getSheet();
100118// Get users as collection
101119$users = User::where('age', '>', 35)->get();
120+
121+ // Write attribute names
122+ $sheet->writeRow(array_keys(User::getAttributes()));
123+
124+ // Write all selected records
102125$sheet->writeData($users);
103126
104127$sheet = $excel->makeSheet('Records');
@@ -186,43 +209,53 @@ $excel->saveTo($testFileName);
186209
187210### Import a Model
188211To import models, you can use method ``` importModel() ``` .
189- By default, the first row is considered to contain the names of the fields
212+ If the first row contains the names of the fields you can apply these using method ``` withHeadings() ```
190213
191214![ import.jpg] ( import.jpg )
192215
193216``` php
194217// Open XLSX-file
195218$excel = Excel::open($file);
196219
197- // Import row to User model
198- $excel->importModel(User::class);
220+ // Import a workbook to User model using the first row as attribute names
221+ $excel->withHeadings()-> importModel(User::class);
199222
200223// Done!!!
201224```
202225You can define the columns or cells from which you will import
203226
204- ![ import2.jpg] ( import2.jpg )
205227``` php
206- // Import row to User model from columns range B:E
207- $excel->importModel(User::class, 'B:D');
228+ // Import row to User model from columns range A:B - only 'name' and 'birthday'
229+ $excel->withHeadings()->importModel(User::class, 'A:B');
230+ ```
208231
232+ ![ import2.jpg] ( import2.jpg )
233+ ``` php
209234// Import from cells range
210- $excel->importModel(User::class, 'B4:D7');
235+ $excel->withHeadings()-> importModel(User::class, 'B4:D7');
211236
212237// Define top left cell only
213- $excel->importModel(User::class, 'B4');
238+ $excel->withHeadings()-> importModel(User::class, 'B4');
214239```
215- In the last two examples, we also assume that the first row of imported data (row 3 )
216- is the names of the fields .
240+ In the last two examples, we also assume that the first row of imported data (row 4 )
241+ is the names of the attributes .
217242
218- However, you can set the correspondence between columns and field names yourself.
219- Then the first line of the imported data will be records for the model.
243+ ### Mapping Import Data
244+
245+ However, you can set the correspondence between columns and field names yourself.
220246
221247``` php
222248// Import row to User model from columns range B:E
223- $excel->importModel(User::class, 'B:D', ['B' => 'name', 'C' => 'birthday', 'D' => 'random']);
249+ $excel->mapping(function ($record) {
250+ return [
251+ 'id' => $record['A'], 'name' => $record['B'], 'birthday' => $record['C'], 'random' => $record['D'],
252+ ];
253+ })->importModel(User::class, 'B:D');
224254
225255// Define top left cell only
256+ $excel->mapping(['B' => 'name', 'C' => 'birthday', 'D' => 'random'])->importModel(User::class, 'B5');
257+
258+ // Define top left cell only (shorter way)
226259$excel->importModel(User::class, 'B5', ['B' => 'name', 'C' => 'birthday', 'D' => 'random']);
227260```
228261
0 commit comments