Skip to content

Commit fdaff9e

Browse files
committed
upd import
1 parent f70fee4 commit fdaff9e

File tree

7 files changed

+78
-26
lines changed

7 files changed

+78
-26
lines changed

README.md

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# FastExcelLaravel
1+
FastExcelLaravel
22
Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
33
(wrapper for [FastExcelWriter](https://packagist.org/packages/avadim/fast-excel-writer))
44

@@ -100,7 +100,7 @@ $sheet->writeData(function () {
100100

101101
### Advanced Usage for Data Export
102102

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
104104

105105
```php
106106
$excel = \Excel::create('Users');
@@ -149,11 +149,65 @@ $excel->saveTo($testFileName);
149149
## Import Data
150150

151151
### 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+
![import.jpg](import.jpg)
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']);
155187
```
156188

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+
157211
## Do you want to support FastExcelLaravel?
158212

159213
if you find this package useful you can support and donate to me for a cup of coffee:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php": ">=7.4",
2323
"ext-json": "*",
2424
"avadim/fast-excel-writer": "^4.1",
25-
"avadim/fast-excel-reader": "^2.3",
25+
"avadim/fast-excel-reader": "main-dev",
2626
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
2727
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0"
2828
},

import.jpg

49.1 KB
Loading

src/FastExcelLaravel/SheetReader.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@ class SheetReader extends \avadim\FastExcelReader\Sheet
2222
*/
2323
public function importModel($modelClass, $address = null, $columns = null): SheetReader
2424
{
25-
$resultMode = 0;
26-
if ($columns === true) {
27-
$resultMode = \avadim\FastExcelReader\Excel::KEYS_FIRST_ROW;
25+
$resultMode = \avadim\FastExcelReader\Excel::KEYS_FIRST_ROW;
26+
if ($columns === false) {
27+
$resultMode = 0;
2828
$columns = [];
2929
}
30-
elseif ($columns === null && $address === true) {
31-
$columns = [];
32-
$resultMode = \avadim\FastExcelReader\Excel::KEYS_FIRST_ROW;
30+
elseif ($columns) {
31+
$resultMode = 0;
3332
}
3433
if ($address && is_string($address)) {
3534
$this->setReadArea($address);
3635
}
37-
$tz = date_default_timezone_get();
3836
foreach ($this->nextRow($columns, $resultMode) as $rowData) {
3937
/** @var Model $model */
4038
$model = new $modelClass;

tests/FastExcelLaravelTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,31 +316,31 @@ public function testLoadModel()
316316

317317
FakeModel::$storage = [];
318318
$excel->importModel(FakeModel::class);
319-
$this->assertCount(4, FakeModel::$storage);
320-
$this->assertNull(FakeModel::$storage[0]->name);
321-
322-
FakeModel::$storage = [];
323-
$excel->importModel(FakeModel::class, true);
324319
$this->assertCount(3, FakeModel::$storage);
325320
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
326321

327322
FakeModel::$storage = [];
328-
$excel->importModel(FakeModel::class, 'b2');
329-
$this->assertCount(3, FakeModel::$storage);
330-
$this->assertNull(FakeModel::$storage[0]->name);
323+
$excel->setDateFormat('Y-m-d');
324+
$excel->importModel(FakeModel::class, 'B4', ['A' => 'foo', 'B' => 'bar', 'C' => 'int']);
325+
$this->assertEquals('1753-01-31', FakeModel::$storage[0]->bar);
326+
327+
$testFileName = 'test_model2.xlsx';
328+
$excel = Excel::open(storage_path($testFileName));
331329

332330
FakeModel::$storage = [];
333-
$excel->importModel(FakeModel::class, 'b1', true);
331+
$excel->importModel(FakeModel::class, 'b4');
334332
$this->assertCount(3, FakeModel::$storage);
335333
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
336334

337335
FakeModel::$storage = [];
338-
$excel->setDateFormat('Y-m-d');
339-
$excel->importModel(FakeModel::class, 'c4', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
340-
$this->assertEquals('1753-01-31', FakeModel::$storage[0]->bar);
336+
$excel->importModel(FakeModel::class, 'b5:d5', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
337+
$this->assertCount(1, FakeModel::$storage);
338+
$this->assertEquals('James Bond', FakeModel::$storage[0]->foo);
339+
$this->assertFalse(isset(FakeModel::$storage[1]));
341340

342341
FakeModel::$storage = [];
343-
$excel->importModel(FakeModel::class, 'b2', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
342+
$excel->setDateFormat('Y-m-d');
343+
$excel->importModel(FakeModel::class, 'b5', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
344344
$this->assertCount(3, FakeModel::$storage);
345345
$this->assertEquals('Captain Jack Sparrow', FakeModel::$storage[2]->foo);
346346
$this->assertEquals('1753-01-31', FakeModel::$storage[2]->bar);

tests/test_storage/test_model.xlsx

-289 Bytes
Binary file not shown.
11.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)