Skip to content

Commit fdd54a7

Browse files
committed
minor update
1 parent d205efc commit fdd54a7

File tree

7 files changed

+80
-50
lines changed

7 files changed

+80
-50
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ $sheet->exportModel(Users::class);
6767
$excel->saveTo('path/file.xlsx');
6868
```
6969
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
70+
7071
```php
7172

7273
// Create workbook with sheet named 'Users'
7374
$excel = \Excel::create('Users');
7475

7576
// Write users with field names in the first row
76-
$sheet->withHeaders()
77+
$sheet->withHeadings()
7778
->applyFontStyleBold()
7879
->applyBorder('thin')
7980
->exportModel(Users::class);
@@ -161,6 +162,9 @@ $area->withRange('a4:d5')
161162
// Write area to sheet
162163
$sheet->writeAreas();
163164

165+
// You can set value formats for some fields
166+
$sheet->setFieldFormats(['birthday' => '@date', 'number' => '@integer']);
167+
164168
// Write data to sheet
165169
$sheet->writeData($data);
166170

@@ -187,15 +191,17 @@ $excel->importModel(User::class);
187191
// Done!!!
188192
```
189193
You can define the columns or cells from which you will import
194+
195+
![import2.jpg](import2.jpg)
190196
```php
191197
// Import row to User model from columns range B:E
192-
$excel->importModel(User::class, 'B:E');
198+
$excel->importModel(User::class, 'B:D');
193199

194200
// Import from cells range
195-
$excel->importModel(User::class, 'B3:E8');
201+
$excel->importModel(User::class, 'B4:D7');
196202

197203
// Define top left cell only
198-
$excel->importModel(User::class, 'B3');
204+
$excel->importModel(User::class, 'B4');
199205
```
200206
In the last two examples, we also assume that the first row of imported data (row 3)
201207
is the names of the fields.
@@ -205,10 +211,10 @@ Then the first line of the imported data will be records for the model.
205211

206212
```php
207213
// Import row to User model from columns range B:E
208-
$excel->importModel(User::class, 'B:E', ['B' => 'name', 'C' => 'birthday', 'D' => 'random', 'E' => 'something']);
214+
$excel->importModel(User::class, 'B:D', ['B' => 'name', 'C' => 'birthday', 'D' => 'random']);
209215

210216
// Define top left cell only
211-
$excel->importModel(User::class, 'B3', ['B' => 'name', 'C' => 'birthday', 'D' => 'random', 'E' => 'something']);
217+
$excel->importModel(User::class, 'B5', ['B' => 'name', 'C' => 'birthday', 'D' => 'random']);
212218
```
213219

214220
### Advanced Usage for Data Import

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"require": {
2222
"php": ">=7.4",
2323
"ext-json": "*",
24-
"avadim/fast-excel-writer": "^4.1",
25-
"avadim/fast-excel-reader": "main-dev",
24+
"avadim/fast-excel-writer": "^4.1.2",
25+
"avadim/fast-excel-reader": "^2.3.2",
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

-28.5 KB
Loading

import2.jpg

23.8 KB
Loading

src/FastExcelLaravel/ExcelWriter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class ExcelWriter extends \avadim\FastExcelWriter\Excel
88
{
9+
/** @var array SheetWriter[] */
10+
protected array $sheets = [];
11+
912
/**
1013
* @param string|array $sheets
1114
* @param array|null $options
@@ -15,7 +18,7 @@ class ExcelWriter extends \avadim\FastExcelWriter\Excel
1518
public static function create($sheets = null, ?array $options = []): ExcelWriter
1619
{
1720
if (empty($options['temp_dir'])) {
18-
$tempDir = storage_path('app/tmp/excel');
21+
$tempDir = storage_path('app/tmp/fast-excel');
1922
if(!\File::isDirectory($tempDir)) {
2023
\File::makeDirectory($tempDir, 0777, true, true);
2124
}

src/FastExcelLaravel/SheetWriter.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace avadim\FastExcelLaravel;
44

55
use avadim\FastExcelWriter\Sheet;
6+
use avadim\FastExcelWriter\Style;
67
use Illuminate\Support\Collection;
78

89
class SheetWriter extends Sheet
910
{
10-
private ?array $headers = null;
11+
private array $headers = [];
1112
private int $dataRowCount = 0;
1213

1314
protected function _toArray($record)
@@ -36,9 +37,9 @@ protected function _writeHeader($record)
3637
$this->headers['header_values'] = $this->headers['header_keys'];
3738
}
3839

39-
$row = array_combine($this->headers['header_keys'], $this->headers['header_values']);
40+
//$row = array_combine($this->headers['header_keys'], $this->headers['header_values']);
4041
$row = $this->headers['header_values'];
41-
$this->writeHeader($row, $this->headers['rowStyle'], $this->headers['colStyles']);
42+
$this->writeHeader($row, $this->headers['row_style'], $this->headers['col_styles']);
4243
++$this->dataRowCount;
4344
}
4445

@@ -48,34 +49,35 @@ public function writeRow(array $rowValues = [], array $rowStyle = null, array $c
4849
$rowData = [];
4950
foreach ($this->headers['header_keys'] as $key) {
5051
if (isset($rowValues[$key])) {
51-
$rowData[] = $rowValues[$key];
52+
$rowData[$key] = $rowValues[$key];
5253
}
5354
else {
5455
$rowData[] = null;
5556
}
5657
}
5758
}
5859
else {
59-
$rowData = array_values($rowValues);
60+
$rowData = $rowValues;
6061
}
62+
6163
return parent::writeRow($rowData, $rowStyle, $cellStyles);
6264
}
6365

6466
/**
6567
* @param $data
6668
* @param array|null $rowStyle
67-
* @param array|null $cellStyles
69+
* @param array|null $colStyles
6870
*
6971
* @return $this
7072
*/
71-
public function writeData($data, array $rowStyle = null, array $cellStyles = null): SheetWriter
73+
public function writeData($data, array $rowStyle = null, array $colStyles = null): SheetWriter
7274
{
7375
if (is_array($data) || ($data instanceof Collection)) {
7476
foreach ($data as $record) {
7577
if ($this->dataRowCount === 0 && $this->headers) {
7678
$this->_writeHeader($record);
7779
}
78-
$this->writeRow($this->_toArray($record), $rowStyle, $cellStyles);
80+
$this->writeRow($this->_toArray($record), $rowStyle, $colStyles);
7981
++$this->dataRowCount;
8082
}
8183
}
@@ -84,7 +86,7 @@ public function writeData($data, array $rowStyle = null, array $cellStyles = nul
8486
if ($this->dataRowCount === 0 && $this->headers) {
8587
$this->_writeHeader($record);
8688
}
87-
$this->writeRow($this->_toArray($record), $rowStyle, $cellStyles);
89+
$this->writeRow($this->_toArray($record), $rowStyle, $colStyles);
8890
++$this->dataRowCount;
8991
}
9092
}
@@ -95,22 +97,22 @@ public function writeData($data, array $rowStyle = null, array $cellStyles = nul
9597
/**
9698
* @param $model
9799
* @param array|null $rowStyle
98-
* @param array|null $cellStyles
100+
* @param array|null $colStyles
99101
*
100102
* @return $this
101103
*/
102-
public function exportModel($model, array $rowStyle = null, array $cellStyles = null): SheetWriter
104+
public function exportModel($model, array $rowStyle = null, array $colStyles = null): SheetWriter
103105
{
104106
$this->writeData(static function() use ($model) {
105107
foreach ($model::cursor() as $user) {
106108
yield $user;
107109
}
108-
}, $rowStyle, $cellStyles);
110+
}, $rowStyle, $colStyles);
109111

110112
return $this;
111113
}
112114

113-
public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array $colStyles = []): SheetWriter
115+
public function withHeadings(?array $headers = [], ?array $rowStyle = [], ?array $colStyles = []): SheetWriter
114116
{
115117
$headerKeys = $headerValues = [];
116118
if ($headers) {
@@ -128,8 +130,8 @@ public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array
128130
$this->headers = [
129131
'header_keys' => $headerKeys,
130132
'header_values' => $headerValues,
131-
'rowStyle' => $rowStyle,
132-
'colStyles' => $colStyles,
133+
'row_style' => $rowStyle,
134+
'col_styles' => $colStyles,
133135
];
134136
$this->lastTouch['ref'] = 'row';
135137

tests/FastExcelLaravelTest.php

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ protected function getDataArray(): array
8686
{
8787
$id = 0;
8888
return [
89-
['id' => $id++, 'integer' => 4573, 'date' => '1900-02-14', 'name' => 'James Bond'],
90-
['id' => $id++, 'integer' => 982630, 'date' => '2179-08-12', 'name' => 'Ellen Louise Ripley'],
91-
['id' => $id++, 'integer' => 7239, 'date' => '1753-01-31', 'name' => 'Captain Jack Sparrow'],
89+
['id' => ++$id, 'integer' => 4573, 'date' => '1900-02-14', 'name' => 'James Bond'],
90+
['id' => ++$id, 'integer' => 982630, 'date' => '2179-08-12', 'name' => 'Ellen Louise Ripley'],
91+
['id' => ++$id, 'integer' => 7239, 'date' => '1753-01-31', 'name' => 'Captain Jack Sparrow'],
9292
];
9393
}
9494

@@ -112,7 +112,7 @@ protected function read($testFileName)
112112
}
113113

114114

115-
protected function startTest($testFileName, $sheets = []): ExcelWriter
115+
protected function startExportTest($testFileName, $sheets = []): ExcelWriter
116116
{
117117
if (file_exists($testFileName)) {
118118
unlink($testFileName);
@@ -124,7 +124,7 @@ protected function startTest($testFileName, $sheets = []): ExcelWriter
124124
return Excel::create($sheets);
125125
}
126126

127-
protected function endTest($testFileName)
127+
protected function endExportTest($testFileName)
128128
{
129129
$this->excelReader = null;
130130
$this->cells = [];
@@ -137,10 +137,10 @@ protected function endTest($testFileName)
137137
}
138138
}
139139

140-
public function testArray()
140+
public function testExportArray()
141141
{
142142
$testFileName = __DIR__ . '/test1.xlsx';
143-
$excel = $this->startTest($testFileName);
143+
$excel = $this->startExportTest($testFileName);
144144

145145
/** @var SheetWriter $sheet */
146146
$sheet = $excel->getSheet();
@@ -153,19 +153,19 @@ public function testArray()
153153

154154
$this->assertEquals(array_values($data[0]), $this->getValues('A1', 'B1', 'C1', 'D1'));
155155

156-
$this->endTest($testFileName);
156+
$this->endExportTest($testFileName);
157157
}
158158

159-
public function testArrayWithHeaders()
159+
public function testExportArrayWithHeaders()
160160
{
161161
$testFileName = __DIR__ . '/test2.xlsx';
162-
$excel = $this->startTest($testFileName);
162+
$excel = $this->startExportTest($testFileName);
163163

164164
/** @var SheetWriter $sheet */
165165
$sheet = $excel->getSheet();
166166

167167
$data = $this->getDataArray();
168-
$sheet->withHeaders()->writeData($data);
168+
$sheet->withHeadings()->writeData($data);
169169
$excel->save($testFileName);
170170

171171
$this->read($testFileName);
@@ -174,13 +174,13 @@ public function testArrayWithHeaders()
174174
$this->assertEquals(array_keys($row), $this->getValues('A1', 'B1', 'C1', 'D1'));
175175
$this->assertEquals(array_values($row), $this->getValues('A3', 'B3', 'C3', 'D3'));
176176

177-
$this->endTest($testFileName);
177+
$this->endExportTest($testFileName);
178178
}
179179

180-
public function testCollection()
180+
public function testExportCollection()
181181
{
182182
$testFileName = __DIR__ . '/test3.xlsx';
183-
$excel = $this->startTest($testFileName);
183+
$excel = $this->startExportTest($testFileName);
184184

185185
/** @var SheetWriter $sheet */
186186
$sheet = $excel->getSheet();
@@ -193,18 +193,18 @@ public function testCollection()
193193

194194
$this->assertEquals(array_values($data[0]), $this->getValues('A1', 'B1', 'C1', 'D1'));
195195

196-
$this->endTest($testFileName);
196+
$this->endExportTest($testFileName);
197197
}
198198

199-
public function testCollectionWithHeaders()
199+
public function testExportCollectionWithHeaders()
200200
{
201201
$testFileName = 'test4.xlsx';
202-
$excel = $this->startTest($testFileName);
202+
$excel = $this->startExportTest($testFileName);
203203

204204
/** @var SheetWriter $sheet */
205205
$sheet = $excel->getSheet();
206206

207-
$sheet->withHeaders(['date', 'name'])
207+
$sheet->withHeadings(['date', 'name'])
208208
->applyFontStyleBold()
209209
->applyBorder('thin')
210210
->writeData(collect($this->getDataCollectionStd()));
@@ -214,13 +214,13 @@ public function testCollectionWithHeaders()
214214

215215
$this->assertEquals(['1753-01-31', 'Captain Jack Sparrow', null, null], $this->getValues('A4', 'B4', 'C4', 'D4'));
216216

217-
$this->endTest($testFileName);
217+
$this->endExportTest($testFileName);
218218
}
219219

220-
public function testMultipleSheets()
220+
public function testExportMultipleSheets()
221221
{
222222
$testFileName = 'test5.xlsx';
223-
$excel = $this->startTest($testFileName);
223+
$excel = $this->startExportTest($testFileName);
224224

225225
$sheet = $excel->makeSheet('Collection');
226226
$collection = collect([
@@ -261,13 +261,13 @@ public function testMultipleSheets()
261261
$this->cells = $this->excelReader->readRows(false, null, true);
262262
$this->assertEquals(9, $this->getValue('C3'));
263263

264-
$this->endTest($testFileName);
264+
$this->endExportTest($testFileName);
265265
}
266266

267-
public function testAdvanced()
267+
public function testExportAdvanced()
268268
{
269269
$testFileName = 'test6.xlsx';
270-
$excel = $this->startTest($testFileName);
270+
$excel = $this->startExportTest($testFileName);
271271

272272
/** @var SheetWriter $sheet */
273273
$sheet = $excel->getSheet();
@@ -305,10 +305,10 @@ public function testAdvanced()
305305

306306
$this->assertEquals([982630, '2179-08-12', 'Ellen Louise Ripley', null], $this->getValues('B7', 'C7', 'D7', 'e7'));
307307

308-
$this->endTest($testFileName);
308+
$this->endExportTest($testFileName);
309309
}
310310

311-
public function testLoadModel()
311+
public function testImportModel()
312312
{
313313
$testFileName = 'test_model.xlsx';
314314
$excel = Excel::open(storage_path($testFileName));
@@ -357,4 +357,23 @@ public function testLoadModel()
357357
$this->assertEquals('Ellen Louise Ripley', $result[6]['B']);
358358
$this->assertEquals('Captain Jack Sparrow', $result[7]['B']);
359359
}
360+
361+
362+
public function testExportArray0()
363+
{
364+
$testFileName = __DIR__ . '/test0.xlsx';
365+
$excel = $this->startExportTest($testFileName);
366+
367+
/** @var SheetWriter $sheet */
368+
$sheet = $excel->getSheet();
369+
370+
$data = $this->getDataArray();
371+
$sheet->withHeadings()->setFieldFormats(['date' => '@date'])->writeData($data);
372+
$excel->save($testFileName);
373+
374+
$this->assertTrue(file_exists($testFileName));
375+
376+
//$this->endExportTest($testFileName);
377+
}
378+
360379
}

0 commit comments

Comments
 (0)