Skip to content

Commit 7637fe8

Browse files
committed
added Custom Formatting Values docs
1 parent b8f3133 commit 7637fe8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Custom Formatting Values
2+
3+
By default Laravel Excel uses PhpSpreadsheet's default value binder to intelligently format a cells value when reading it. You may override this behavior by implementing the `WithCustomValueBinder` concern and the `bindValue` method. Your import class may also extend `PHPExcel_Cell_DefaultValueBinder` to return the default behavior.
4+
5+
```php
6+
namespace App\Imports;
7+
8+
use PHPExcel_Cell_DataType;
9+
use PHPExcel_Cell_DefaultValueBinder;
10+
use PhpOffice\PhpSpreadsheet\Cell\Cell;
11+
use Maatwebsite\Excel\Concerns\ToModel;
12+
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
13+
14+
class UsersImport extends PHPExcel_Cell_DefaultValueBinder implements WithCustomValueBinder, ToModel
15+
{
16+
public function bindValue(Cell $cell, $value)
17+
{
18+
if (is_numeric($value)) {
19+
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
20+
21+
return true;
22+
}
23+
24+
// else return default behavior
25+
return parent::bindValue($cell, $value);
26+
}
27+
28+
public function model(array $row)
29+
{
30+
return new User([
31+
'name' => $row['0'],
32+
'email' => $row['1']
33+
]);
34+
}
35+
}
36+
```
37+
38+
Available PHPExcel_Cell_DataType's are `TYPE_STRING`, `TYPE_FORMULA`, `TYPE_NUMERIC`, `TYPE_BOOL`, `TYPE_NULL`, `TYPE_INLINE` and `TYPE_ERROR`

0 commit comments

Comments
 (0)