CsvExporter
API reference › CSV Export › CsvExporter
CsvExporter allows export of data into CSV files.
It supports exporting of the DataProviderInterface and QueryInterface instances.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$batchSize | int | The number of records to be fetched in each batch. Default: 100. | CsvExporter |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$columns | list<string|Column> | Grid column configuration. Default: []. | CsvExporter |
$csvFileConfig | array<string, | Configuration for CsvFile instances created in process. Default: []. | CsvExporter |
$dataProvider | yii\| | The data provider for the view. Default: null. | CsvExporter |
$emptyCell | string | The HTML display when the content of a cell is empty. Default: ''. | CsvExporter |
$formatter | yii\ | The formatter used to format model attribute values into displayable texts. | CsvExporter |
$maxEntriesPerFile | int| | The maximum entries count allowed in single file. Default: null. | CsvExporter |
$nullDisplay | string | The text to be displayed when formatting a null data value. Default: ''. | CsvExporter |
$query | yii\| | The data source query. Default: null. | CsvExporter |
$resultConfig | array<string, | Configuration for ExportResult instance created in process result. Default: []. | CsvExporter |
$showFooter | bool | Whether to show the footer section of the sheet. Default: false. | CsvExporter |
$showHeader | bool | Whether to show the header section of the sheet. Default: true. | CsvExporter |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
export() | Writes the configured data source to one or more finalized CSV files. | CsvExporter |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
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\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
setFormatter() | Sets the formatter for column values. Configuration is resolved on first use. | CsvExporter |
trigger() | Triggers an event. | yii\ |
composeBodyRow() protected | Collects the rendered values of one data row in column order. | CsvExporter |
composeFooterRow() protected | Collects the footer cell values in column order. | CsvExporter |
composeHeaderRow() protected | Collects the header cell values in column order. | CsvExporter |
createDataColumn() protected | Creates a DataColumn object based on a string in the format of “attribute:format:label”. | CsvExporter |
initColumns() protected | Builds a separate schema for this export. | CsvExporter |
Property details
Section titled “Property details”$batchSize
Section titled “$batchSize ”public int $batchSize = 100The number of records to be fetched in each batch.
This property takes effect only in case of $query usage.
$columns
Section titled “$columns ”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.
$dataProvider
Section titled “$dataProvider ”public yii\data\DataProviderInterface|null $dataProvider = nullThe data provider for the view.
This property can be omitted in case $query is set.
$emptyCell
Section titled “$emptyCell ”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.
$maxEntriesPerFile
Section titled “$maxEntriesPerFile ”public int|null $maxEntriesPerFile = nullThe 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.
$query
Section titled “$query ”public yii\db\QueryInterface|null $query = nullThe data source query.
Note: this field will be ignored in case $dataProvider is set.
Method details
Section titled “Method details”export()
Section titled “export() ”public function export(): yii2\extensions\csvexport\ExportResultWrites the configured data source to one or more finalized CSV files.
Returns
ExportResult: The result that owns the files and handles delivery.
Throws
yii\base\InvalidConfigException: if the data source, columns, file limit, writer or result configuration is invalid.
getFormatter()
Section titled “getFormatter() ”public function getFormatter(): yii\i18n\FormatterResolves the configured formatter or uses the Yii application’s formatter.
Returns
yii\i18n\Formatter: The formatter, cached after the first resolution.
Throws
yii\base\InvalidConfigException: if no formatter can be resolved.
setFormatter()
Section titled “setFormatter() ”public function setFormatter(array<string, mixed>|yii\i18n\Formatter $formatter): voidSets the formatter for column values. Configuration is resolved on first use.
Parameters
| Parameter | Type | Description |
|---|---|---|
$formatter | array<string,yii\ | Formatter configuration or instance. |
composeBodyRow()
Section titled “composeBodyRow() ”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
| Parameter | Type | Description |
|---|---|---|
$columns | list<Column> | Columns of this export. |
$model | mixed | Data row from the effective source. |
$key | mixed | Source key, when available. |
$index | int | Zero-based row index across the export. |
Returns
list<mixed>: One rendered cell for each column.
composeFooterRow()
Section titled “composeFooterRow() ”protected function composeFooterRow(list<yii2\extensions\csvexport\Column> $columns): list<string>Collects the footer cell values in column order.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columns | list<Column> | Columns of this export. |
Returns
list<string>: One footer cell for each column.
composeHeaderRow()
Section titled “composeHeaderRow() ”protected function composeHeaderRow(list<yii2\extensions\csvexport\Column> $columns): list<string>Collects the header cell values in column order.
Parameters
| Parameter | Type | Description |
|---|---|---|
$columns | list<Column> | Columns of this export. |
Returns
list<string>: One header cell for each column.
createDataColumn()
Section titled “createDataColumn() ”protected function createDataColumn(string $text): yii2\extensions\csvexport\DataColumnCreates a DataColumn object based on a string in the format of “attribute:format:label”.
Parameters
| Parameter | Type | Description |
|---|---|---|
$text | string | The column specification string. |
Returns
DataColumn: The column instance
Throws
yii\base\InvalidConfigException: if the column specification is invalid.
initColumns()
Section titled “initColumns() ”protected function initColumns(mixed $model): list<yii2\extensions\csvexport\Column>Builds a separate schema for this export.
Parameters
| Parameter | Type | Description |
|---|---|---|
$model | mixed | First data row, used to infer columns and labels, or null for an empty source. |
Returns
list<Column>: Visible columns bound to this exporter.
Throws
yii\base\InvalidConfigException: if a column configuration or an inferred column name is invalid.