Skip to content
PHP Ecosystem
Explore

CsvExporter

API reference › CSV Export › CsvExporter

classyii2\extensions\csvexport\CsvExporter
Extendsyii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Sourcesrc/CsvExporter.php

CsvExporter allows export of data into CSV files.

It supports exporting of the DataProviderInterface and QueryInterface instances.

PropertyTypeDescriptionDefined by
$batchSizeintThe number of records to be fetched in each batch. Default: 100.CsvExporter
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$columnslist<string|array<string, mixed>|Column>Grid column configuration. Default: [].CsvExporter
$csvFileConfigarray<string, mixed>Configuration for CsvFile instances created in process. Default: [].CsvExporter
$dataProvideryii\data\DataProviderInterface|nullThe data provider for the view. Default: null.CsvExporter
$emptyCellstringThe HTML display when the content of a cell is empty. Default: ''.CsvExporter
$formatteryii\i18n\FormatterThe formatter used to format model attribute values into displayable texts.CsvExporter
$maxEntriesPerFileint|nullThe maximum entries count allowed in single file. Default: null.CsvExporter
$nullDisplaystringThe text to be displayed when formatting a null data value. Default: ''.CsvExporter
$queryyii\db\QueryInterface|nullThe data source query. Default: null.CsvExporter
$resultConfigarray<string, mixed>Configuration for ExportResult instance created in process result. Default: [].CsvExporter
$showFooterboolWhether to show the footer section of the sheet. Default: false.CsvExporter
$showHeaderboolWhether to show the header section of the sheet. Default: true.CsvExporter
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
export()Writes the configured data source to one or more finalized CSV files.CsvExporter
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getFormatter()Resolves the configured formatter or uses the Yii application’s formatter.CsvExporter
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.yii\base\BaseObject
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
setFormatter()Sets the formatter for column values. Configuration is resolved on first use.CsvExporter
trigger()Triggers an event.yii\base\Component
composeBodyRow() protectedCollects the rendered values of one data row in column order.CsvExporter
composeFooterRow() protectedCollects the footer cell values in column order.CsvExporter
composeHeaderRow() protectedCollects the header cell values in column order.CsvExporter
createDataColumn() protectedCreates a DataColumn object based on a string in the format of “attribute:format:label”.CsvExporter
initColumns() protectedBuilds a separate schema for this export.CsvExporter
public int $batchSize = 100

The number of records to be fetched in each batch.

This property takes effect only in case of $query usage.

public list<string|array<string, mixed>|yii2\extensions\csvexport\Column> $columns = []

Grid column configuration.

Each array element represents the configuration for one particular grid column.

If a column is of class DataColumn, the “class” element can be omitted.

public yii\data\DataProviderInterface|null $dataProvider = null

The data provider for the view.

This property can be omitted in case $query is set.

public string $emptyCell = ''

The HTML display when the content of a cell is empty.

This property is used to render cells that have no defined content, for example empty footer or filter cells.

Note that this is not used by the DataColumn if a data item is null. In that case the $nullDisplay property will be used to indicate an empty data value.

public int|null $maxEntriesPerFile = null

The maximum entries count allowed in single file.

You may use this parameter to split large export results into several smaller files.

For example: ‘Open Office’ and ‘MS Excel 97-2003’ allows maximum 65536 rows per CSV file, ‘MS Excel 2007’ - 1048576.

In case several files are generated during export the results will be archived into a single file.

If this value is empty - no limit checking will be performed.

public yii\db\QueryInterface|null $query = null

The data source query.

Note: this field will be ignored in case $dataProvider is set.

public function export(): yii2\extensions\csvexport\ExportResult

Writes the configured data source to one or more finalized CSV files.

Returns

ExportResult: The result that owns the files and handles delivery.

Throws

public function getFormatter(): yii\i18n\Formatter

Resolves the configured formatter or uses the Yii application’s formatter.

Returns

yii\i18n\Formatter: The formatter, cached after the first resolution.

Throws

public function setFormatter(array<string, mixed>|yii\i18n\Formatter $formatter): void

Sets the formatter for column values. Configuration is resolved on first use.

Parameters

ParameterTypeDescription
$formatterarray<string, mixed>|yii\i18n\FormatterFormatter configuration or instance.
protected function composeBodyRow(list<yii2\extensions\csvexport\Column> $columns, mixed $model, mixed $key, int $index): list<mixed>

Collects the rendered values of one data row in column order.

Parameters

ParameterTypeDescription
$columnslist<Column>Columns of this export.
$modelmixedData row from the effective source.
$keymixedSource key, when available.
$indexintZero-based row index across the export.

Returns

list<mixed>: One rendered cell for each column.

protected function composeFooterRow(list<yii2\extensions\csvexport\Column> $columns): list<string>

Collects the footer cell values in column order.

Parameters

ParameterTypeDescription
$columnslist<Column>Columns of this export.

Returns

list<string>: One footer cell for each column.

protected function composeHeaderRow(list<yii2\extensions\csvexport\Column> $columns): list<string>

Collects the header cell values in column order.

Parameters

ParameterTypeDescription
$columnslist<Column>Columns of this export.

Returns

list<string>: One header cell for each column.

protected function createDataColumn(string $text): yii2\extensions\csvexport\DataColumn

Creates a DataColumn object based on a string in the format of “attribute:format:label”.

Parameters

ParameterTypeDescription
$textstringThe column specification string.

Returns

DataColumn: The column instance

Throws

protected function initColumns(mixed $model): list<yii2\extensions\csvexport\Column>

Builds a separate schema for this export.

Parameters

ParameterTypeDescription
$modelmixedFirst data row, used to infer columns and labels, or null for an empty source.

Returns

list<Column>: Visible columns bound to this exporter.

Throws