Class \avadim\FastExcelLaravel\SheetReader
On this page
SheetReader is a thin Laravel wrapper around a FastExcelReader sheet. It does not extend a concrete
sheet; instead it composes an avadim\FastExcelReader\AbstractSheet and adds the Eloquent-aware import
helpers. Because it relies only on the shared AbstractSheet public API, the same wrapper works for every
format the reader supports (XLSX, XLS, …).
Any method that is not listed below is transparently forwarded to the wrapped AbstractSheet (see
Delegated methods).
Upgrade note. Up to and including 3.x
SheetReaderextended\avadim\FastExcelReader\Sheet. It no longer does, so it is not an instance of that class anymore. Type-hint on\avadim\FastExcelLaravel\SheetReaderinstead.
Own methods:
- withHeadings() -- Set headings for the sheet
- mapping() -- Set mapping callback for the sheet
- importModel() -- Load models from the sheet into the database
- readRows() -- Returns cell values as a two-dimensional array (applies the mapping, if any)
- getSheet() -- Returns the wrapped sheet instance
- __construct()
withHeadings()
public function withHeadings(?array $headers = []): SheetReader
Set headings for the sheet. The first row of the read area is always skipped; if $headers is not empty,
these names are used as attribute names (in column order) instead of the values of the first row. The setting
applies to the next import operation only.
Naming. This is the wrapper's name for the reader's own
withHeader()(avadim\FastExcelReader\AbstractSheet::withHeader()). The wrapper useswithHeadings()to match the Laravel ecosystem convention (as in maatwebsite/excel); the underlying reader calls the same conceptwithHeader().
Parameters
array|null $headers
mapping()
public function mapping($callback): SheetReader
Set a mapping for the sheet. Accepts a callback function (array $row): array or an array of
column => attribute correspondences. The setting applies to importModel() and readRows().
Parameters
\Closure|callable|array $callback
importModel()
public function importModel($modelClass, $address = null, $columns = null, ?int $batchSize = null): SheetReader
Load models from the sheet into the database. The whole import runs in a single transaction on the model's
connection: if any row fails, nothing is imported. By default a new model is filled and saved for each row
(fill() + save()); with $batchSize the filled attributes are inserted in batches, one query per batch
(Eloquent events are not fired, models get no ids; mutators, casts and timestamps are applied).
importModel(User::class) // whole sheet
importModel(User::class, 'B:D') // read columns B:D
importModel(User::class, 'B3') // read area starting at B3
importModel(User::class, 'B3', true) // read area starting at B3, first row as field names
importModel(User::class, batchSize: 1000) // insert rows in batches of 1000
Parameters
$modelClass$address$columnsint|null $batchSize-- insert rows in batches of this size instead of saving each model; must be at least 1
readRows()
public function readRows($columnKeys = [], ?int $resultMode = null, ?bool $styleIdxInclude = null): array
Returns cell values as a two-dimensional array [row][col]. If a mapping is set, it is applied to every row.
Parameters
array|bool|int|null $columnKeysint|null $resultModebool|null $styleIdxInclude
getSheet()
public function getSheet(): \avadim\FastExcelReader\AbstractSheet
Returns the wrapped sheet instance (an avadim\FastExcelReader\Sheet for XLSX or
avadim\FastExcelReader\Xls\XlsSheet for XLS).
Parameters
None
__construct()
public function __construct(\avadim\FastExcelReader\AbstractSheet $sheet, ExcelReader $excel)
Wraps a sheet owned by an ExcelReader. Created internally by
ExcelReader::sheet() / ExcelReader::wrapSheet(); you normally do not construct it directly.
Parameters
\avadim\FastExcelReader\AbstractSheet $sheetExcelReader $excel
Delegated methods
Every reading method not listed above is forwarded to the wrapped AbstractSheet via __call, e.g.
name(), nextRow(), setReadArea(), setReadAreaColumns(), from(), readCells(), readColumns(),
readFirstRow(), getMergedCells(), getHyperlinks() (XLSX only), countRows() / countColumns(), the dimension
helpers, and the image and style helpers. A sheet returned by a delegated call is re-wrapped so fluent chains keep working.
The full list is documented in the underlying FastExcelReader library: https://github.com/aVadim483/fast-excel-reader#readme