Class \avadim\FastExcelLaravel\SheetReader

Source: 4.3.0 · 2d73434

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 SheetReader extended \avadim\FastExcelReader\Sheet. It no longer does, so it is not an instance of that class anymore. Type-hint on \avadim\FastExcelLaravel\SheetReader instead.


Own methods:


withHeadings()


php
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 uses withHeadings() to match the Laravel ecosystem convention (as in maatwebsite/excel); the underlying reader calls the same concept withHeader().

Parameters

  • array|null $headers

mapping()


php
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()


php
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).

php
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
  • $columns
  • int|null $batchSize -- insert rows in batches of this size instead of saving each model; must be at least 1

readRows()


php
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 $columnKeys
  • int|null $resultMode
  • bool|null $styleIdxInclude

getSheet()


php
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()


php
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 $sheet
  • ExcelReader $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


FastExcelLaravel 4.3.0 · Search this library and version.

Loading search…

Could not load search. Please try again.

No results. Try another word or method name.