Skip to content

Commit f70fee4

Browse files
committed
update export/import
1 parent e7e0953 commit f70fee4

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

README.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

87103
See 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

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

src/FastExcelLaravel/ExcelReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public static function open(string $file): ExcelReader
2828
*
2929
* @return $this
3030
*/
31-
public function loadModels(string $modelClass, $address = null, $columns = null): ExcelReader
31+
public function importModel(string $modelClass, $address = null, $columns = null): ExcelReader
3232
{
33-
$this->sheet()->loadModels($modelClass, $address, $columns);
33+
$this->sheet()->importModel($modelClass, $address, $columns);
3434

3535
return $this;
3636
}

src/FastExcelLaravel/SheetReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SheetReader extends \avadim\FastExcelReader\Sheet
2020
*
2121
* @return $this
2222
*/
23-
public function loadModels($modelClass, $address = null, $columns = null): SheetReader
23+
public function importModel($modelClass, $address = null, $columns = null): SheetReader
2424
{
2525
$resultMode = 0;
2626
if ($columns === true) {

src/FastExcelLaravel/SheetWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function writeData($data, array $rowStyle = null, array $cellStyles = nul
9999
*
100100
* @return $this
101101
*/
102-
public function writeModel($model, array $rowStyle = null, array $cellStyles = null): SheetWriter
102+
public function exportModel($model, array $rowStyle = null, array $cellStyles = null): SheetWriter
103103
{
104104
$this->writeData(static function() use ($model) {
105105
foreach ($model::cursor() as $user) {

tests/FastExcelLaravelTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,32 +315,32 @@ public function testLoadModel()
315315
$this->assertEquals('Sheet1', $excel->sheet()->name());
316316

317317
FakeModel::$storage = [];
318-
$excel->loadModels(FakeModel::class);
318+
$excel->importModel(FakeModel::class);
319319
$this->assertCount(4, FakeModel::$storage);
320320
$this->assertNull(FakeModel::$storage[0]->name);
321321

322322
FakeModel::$storage = [];
323-
$excel->loadModels(FakeModel::class, true);
323+
$excel->importModel(FakeModel::class, true);
324324
$this->assertCount(3, FakeModel::$storage);
325325
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
326326

327327
FakeModel::$storage = [];
328-
$excel->loadModels(FakeModel::class, 'b2');
328+
$excel->importModel(FakeModel::class, 'b2');
329329
$this->assertCount(3, FakeModel::$storage);
330330
$this->assertNull(FakeModel::$storage[0]->name);
331331

332332
FakeModel::$storage = [];
333-
$excel->loadModels(FakeModel::class, 'b1', true);
333+
$excel->importModel(FakeModel::class, 'b1', true);
334334
$this->assertCount(3, FakeModel::$storage);
335335
$this->assertEquals('James Bond', FakeModel::$storage[0]->name);
336336

337337
FakeModel::$storage = [];
338338
$excel->setDateFormat('Y-m-d');
339-
$excel->loadModels(FakeModel::class, 'c4', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
339+
$excel->importModel(FakeModel::class, 'c4', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
340340
$this->assertEquals('1753-01-31', FakeModel::$storage[0]->bar);
341341

342342
FakeModel::$storage = [];
343-
$excel->loadModels(FakeModel::class, 'b2', ['B' => 'foo', 'C' => 'bar', 'D' => 'int']);
343+
$excel->importModel(FakeModel::class, 'b2', ['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);

0 commit comments

Comments
 (0)