|
1 | | -# FastExcelLaravel |
| 1 | + FastExcelLaravel |
2 | 2 | Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel |
3 | 3 | (wrapper for [FastExcelWriter](https://packagist.org/packages/avadim/fast-excel-writer)) |
4 | 4 |
|
@@ -100,7 +100,7 @@ $sheet->writeData(function () { |
100 | 100 |
|
101 | 101 | ### Advanced Usage for Data Export |
102 | 102 |
|
103 | | -See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme |
| 103 | +See detailed documentation for avadim/fast-excel-writer here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme |
104 | 104 |
|
105 | 105 | ```php |
106 | 106 | $excel = \Excel::create('Users'); |
@@ -149,11 +149,65 @@ $excel->saveTo($testFileName); |
149 | 149 | ## Import Data |
150 | 150 |
|
151 | 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) |
| 152 | +To import models, you can use method ```importModel()```. |
| 153 | +By default, the first row is considered to contain the names of the fields |
| 154 | + |
| 155 | +```php |
| 156 | +// Open XLSX-file |
| 157 | +$excel = Excel::open($file); |
| 158 | + |
| 159 | +// Import row to User model |
| 160 | +$excel->importModel(User::class); |
| 161 | + |
| 162 | +// Done!!! |
| 163 | +``` |
| 164 | +You can define the columns or cells from which you will import |
| 165 | +```php |
| 166 | +// Import row to User model from columns range B:E |
| 167 | +$excel->importModel(User::class, 'B:E'); |
| 168 | + |
| 169 | +// Import from cells range |
| 170 | +$excel->importModel(User::class, 'B3:E8'); |
| 171 | + |
| 172 | +// Define top left cell only |
| 173 | +$excel->importModel(User::class, 'B3'); |
| 174 | +``` |
| 175 | +In the last two examples, we also assume that the first row of imported data (row 3) |
| 176 | +is the names of the fields. |
| 177 | + |
| 178 | +However, you can set the correspondence between columns and field names yourself. |
| 179 | +Then the first line of the imported data will be records for the model. |
| 180 | + |
| 181 | +```php |
| 182 | +// Import row to User model from columns range B:E |
| 183 | +$excel->importModel(User::class, 'B:E', ['B' => 'name', 'C' => 'birthday', 'D' => 'random', 'E' => 'something']); |
| 184 | + |
| 185 | +// Define top left cell only |
| 186 | +$excel->importModel(User::class, 'B3', ['B' => 'name', 'C' => 'birthday', 'D' => 'random', 'E' => 'something']); |
155 | 187 | ``` |
156 | 188 |
|
| 189 | +### Advanced Usage for Data Import |
| 190 | +See detailed documentation for avadim/fast-excel-reader here: https://github.com/aVadim483/fast-excel-reader/tree/master#readme |
| 191 | +```php |
| 192 | +$excel = Excel::open($file); |
| 193 | + |
| 194 | +$sheet = $excel->getSheet('Articles'); |
| 195 | +foreach ($sheet->nextRow() as $rowNum => $rowData) { |
| 196 | + $user = User::create([ |
| 197 | + 'name' => $rowData['B'], |
| 198 | + 'birthday' => new \Carbon($rowData['C']), |
| 199 | + 'password' => bcrypt($rowData['D']), |
| 200 | + ]); |
| 201 | + Article::create([ |
| 202 | + 'user_id' => $user->id, |
| 203 | + 'title' => $rowData['E'], |
| 204 | + 'date' => new \Carbon($rowData['F']), |
| 205 | + 'public' => $rowData['G'] === 'yes', |
| 206 | + ]); |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | + |
157 | 211 | ## Do you want to support FastExcelLaravel? |
158 | 212 |
|
159 | 213 | if you find this package useful you can support and donate to me for a cup of coffee: |
|
0 commit comments