Skip to content

Commit e7e0953

Browse files
committed
import models
1 parent f01611e commit e7e0953

File tree

8 files changed

+164
-13
lines changed

8 files changed

+164
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
.phpunit.result.cache

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
44

55
## Introduction
66

7-
This library is a wrapper for avadim/fast-excel-writer, so it's also lightweight, fast, and requires a minimum of memory.
7+
Exporting data from your Laravel application has never been so fast! Importing models into your Laravel application has never been so easy!
8+
9+
This library is a wrapper for avadim/fast-excel-writer фтв avadim/fast-excel-куфвук, so it's also lightweight, fast, and requires a minimum of memory.
810
Using this library, you can export arrays, collections and models to a XLSX-file from your Laravel application.
911

1012
## Installation
@@ -80,7 +82,7 @@ $sheet->writeData(function () {
8082

8183
```
8284

83-
## Advanced usage
85+
## Advanced usage for data export
8486

8587
See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme
8688

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"PHPExcel",
88
"spreadsheet",
99
"laravel",
10-
"export"
10+
"export",
11+
"import"
1112
],
1213
"type": "library",
1314
"homepage": "https://github.com/aVadim483/fast-excel-laravel",
@@ -18,10 +19,10 @@
1819
}
1920
},
2021
"require": {
21-
"php": "^7.4|^8.1",
22+
"php": ">=7.4",
2223
"ext-json": "*",
23-
"avadim/fast-excel-writer": "^4.0",
24-
"avadim/fast-excel-reader": "^2.2",
24+
"avadim/fast-excel-writer": "^4.1",
25+
"avadim/fast-excel-reader": "^2.3",
2526
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
2627
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0"
2728
},

src/FastExcelLaravel/ExcelReader.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
class ExcelReader extends \avadim\FastExcelReader\Excel
66
{
7+
public static function createSheet(string $sheetName, $sheetId, $file, $path): SheetReader
8+
{
9+
return new SheetReader($sheetName, $sheetId, $file, $path);
10+
}
11+
712
/**
813
* Open XLSX file
914
*
@@ -16,4 +21,17 @@ public static function open(string $file): ExcelReader
1621
return new self($file);
1722
}
1823

24+
/**
25+
* @param string $modelClass
26+
* @param string|bool|null $address
27+
* @param array|bool|null $columns
28+
*
29+
* @return $this
30+
*/
31+
public function loadModels(string $modelClass, $address = null, $columns = null): ExcelReader
32+
{
33+
$this->sheet()->loadModels($modelClass, $address, $columns);
34+
35+
return $this;
36+
}
1937
}

src/FastExcelLaravel/SheetReader.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,46 @@
22

33
namespace avadim\FastExcelLaravel;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
57
class SheetReader extends \avadim\FastExcelReader\Sheet
68
{
7-
public function loadModel($modelClass, $address = null, $columns = null)
9+
/**
10+
* Load models from Excel
11+
* loadModels(User::class)
12+
* loadModels(User::class, true) -- the first row used as a field names
13+
* loadModels(User::class, 'B:D') -- read data from columns B:D
14+
* loadModels(User::class, 'B3') -- read data from area started at B3
15+
* loadModels(User::class, 'B3', true) -- read data from area started at B3 and the first row used as a field names
16+
*
17+
* @param $modelClass
18+
* @param $address
19+
* @param $columns
20+
*
21+
* @return $this
22+
*/
23+
public function loadModels($modelClass, $address = null, $columns = null): SheetReader
824
{
9-
$this->setReadArea($address);
10-
foreach ($this->nextRow() as $rowNum => $rowData) {
25+
$resultMode = 0;
26+
if ($columns === true) {
27+
$resultMode = \avadim\FastExcelReader\Excel::KEYS_FIRST_ROW;
28+
$columns = [];
29+
}
30+
elseif ($columns === null && $address === true) {
31+
$columns = [];
32+
$resultMode = \avadim\FastExcelReader\Excel::KEYS_FIRST_ROW;
33+
}
34+
if ($address && is_string($address)) {
35+
$this->setReadArea($address);
36+
}
37+
$tz = date_default_timezone_get();
38+
foreach ($this->nextRow($columns, $resultMode) as $rowData) {
39+
/** @var Model $model */
1140
$model = new $modelClass;
41+
$model->fill($rowData);
42+
$model->save();
1243
}
44+
45+
return $this;
1346
}
1447
}

tests/FakeModel.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel;
4+
5+
class FakeModel
6+
{
7+
public static array $storage = [];
8+
9+
protected array $attributes = [];
10+
protected array $fillable = [];
11+
12+
public function __construct(array $attributes = [])
13+
{
14+
$this->fill($attributes);
15+
}
16+
17+
public static function create(array $attributes = []): FakeModel
18+
{
19+
return new self($attributes);
20+
}
21+
22+
public function fill(array $attributes): FakeModel
23+
{
24+
$this->attributes = $attributes;
25+
26+
return $this;
27+
}
28+
29+
public function save(array $options = []): bool
30+
{
31+
self::$storage[] = $this;
32+
33+
return true;
34+
}
35+
36+
public function __get($name)
37+
{
38+
return $this->attributes[$name] ?? null;
39+
}
40+
}

tests/FastExcelLaravelTest.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace avadim\FastExcelLaravel;
76

7+
require_once __DIR__ . '/FakeModel.php';
8+
89
use Illuminate\Support\Collection;
9-
use PHPUnit\Framework\TestCase;
1010
use avadim\FastExcelReader\Excel as ExcelReader;
11-
use \Illuminate\Filesystem\Filesystem as File;
1211

13-
//final class FastExcelLaravelTest extends TestCase
1412
final class FastExcelLaravelTest extends \Orchestra\Testbench\TestCase
1513
{
1614
protected ?ExcelReader $excelReader = null;
@@ -24,8 +22,26 @@ protected function setUp(): void
2422
$this->testStorage = __DIR__ . '/test_storage';
2523

2624
app()->useStoragePath($this->testStorage);
25+
$this->setUpDatabase();
26+
}
27+
28+
protected function getEnvironmentSetUp($app)
29+
{
30+
$app['config']->set('database.default', 'sqlite');
31+
$app['config']->set('database.connections.sqlite', [
32+
'driver' => 'sqlite',
33+
'database' => ':memory:',
34+
'prefix' => '',
35+
]);
2736
}
2837

38+
protected function setUpDatabase()
39+
{
40+
//$res = $this->artisan('migrate')->run();
41+
//$this->loadMigrationsFrom(__DIR__ . '/../database/migrations/000_create_test_models_table.php');
42+
}
43+
44+
2945
protected function getValue($cell)
3046
{
3147
preg_match('/^(\w+)(\d+)$/', strtoupper($cell), $m);
@@ -292,4 +308,42 @@ public function testAdvanced()
292308
$this->endTest($testFileName);
293309
}
294310

311+
public function testLoadModel()
312+
{
313+
$testFileName = 'test_model.xlsx';
314+
$excel = Excel::open(storage_path($testFileName));
315+
$this->assertEquals('Sheet1', $excel->sheet()->name());
316+
317+
FakeModel::$storage = [];
318+
$excel->loadModels(FakeModel::class);
319+
$this->assertCount(4, FakeModel::$storage);
320+
$this->assertNull(FakeModel::$storage[0]->name);
321+
322+
FakeModel::$storage = [];
323+
$excel->loadModels(FakeModel::class, true);
324+
$this->assertCount(3, FakeModel::$storage);
325+
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
326+
327+
FakeModel::$storage = [];
328+
$excel->loadModels(FakeModel::class, 'b2');
329+
$this->assertCount(3, FakeModel::$storage);
330+
$this->assertNull(FakeModel::$storage[0]->name);
331+
332+
FakeModel::$storage = [];
333+
$excel->loadModels(FakeModel::class, 'b1', true);
334+
$this->assertCount(3, FakeModel::$storage);
335+
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
336+
337+
FakeModel::$storage = [];
338+
$excel->setDateFormat('Y-m-d');
339+
$excel->loadModels(FakeModel::class, 'c4', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
340+
$this->assertEquals('1753-01-31', FakeModel::$storage[0]->bar);
341+
342+
FakeModel::$storage = [];
343+
$excel->loadModels(FakeModel::class, 'b2', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
344+
$this->assertCount(3, FakeModel::$storage);
345+
$this->assertEquals('Captain Jack Sparrow', FakeModel::$storage[2]->foo);
346+
$this->assertEquals('1753-01-31', FakeModel::$storage[2]->bar);
347+
$this->assertEquals(7239, FakeModel::$storage[2]->int);
348+
}
295349
}

tests/test_storage/test_model.xlsx

9.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)