# CsvExporter

> CsvExporter allows export of data into CSV files.

[API reference](/docs/api/index.md) › [CSV Export](/docs/extensions/csv-export/reference/index.md) › CsvExporter

| class | `yii2\extensions\csvexport\CsvExporter` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/CsvExporter.php`](https://github.com/yii2-extensions/csv-export/blob/main/src/CsvExporter.php#L30) |

CsvExporter allows export of data into CSV files.

It supports exporting of the [`DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md) and [`QueryInterface`](/docs/api/yii/db/queryinterface/index.md) instances.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$batchSize`](#property-batchSize) | `int` | The number of records to be fetched in each batch. Default: `100`. | `CsvExporter` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$columns`](#property-columns) | `list<string\|array<string, mixed>\|`[`Column`](/docs/extensions/csv-export/reference/column/index.md)`>` | Grid column configuration. Default: `[]`. | `CsvExporter` |
| <span id="property-csvFileConfig"></span>`$csvFileConfig` | `array<string, mixed>` | Configuration for [`CsvFile`](/docs/extensions/csv-export/reference/csvfile/index.md) instances created in process. Default: `[]`. | `CsvExporter` |
| [`$dataProvider`](#property-dataProvider) | [`yii\data\DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md)`\|null` | The data provider for the view. Default: `null`. | `CsvExporter` |
| [`$emptyCell`](#property-emptyCell) | `string` | The HTML display when the content of a cell is empty. Default: `''`. | `CsvExporter` |
| <span id="property-formatter"></span>`$formatter` | [`yii\i18n\Formatter`](/docs/api/yii/i18n/formatter/index.md) | The formatter used to format model attribute values into displayable texts. | `CsvExporter` |
| [`$maxEntriesPerFile`](#property-maxEntriesPerFile) | `int\|null` | The maximum entries count allowed in single file. Default: `null`. | `CsvExporter` |
| <span id="property-nullDisplay"></span>`$nullDisplay` | `string` | The text to be displayed when formatting a `null` data value. Default: `''`. | `CsvExporter` |
| [`$query`](#property-query) | [`yii\db\QueryInterface`](/docs/api/yii/db/queryinterface/index.md)`\|null` | The data source query. Default: `null`. | `CsvExporter` |
| <span id="property-resultConfig"></span>`$resultConfig` | `array<string, mixed>` | Configuration for [`ExportResult`](/docs/extensions/csv-export/reference/exportresult/index.md) instance created in process result. Default: `[]`. | `CsvExporter` |
| <span id="property-showFooter"></span>`$showFooter` | `bool` | Whether to show the footer section of the sheet. Default: `false`. | `CsvExporter` |
| <span id="property-showHeader"></span>`$showHeader` | `bool` | Whether to show the header section of the sheet. Default: `true`. | `CsvExporter` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`export()`](#method-export) | Writes the configured data source to one or more finalized CSV files. | `CsvExporter` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getFormatter()`](#method-getFormatter) | Resolves the configured formatter or uses the Yii application's formatter. | `CsvExporter` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`setFormatter()`](#method-setFormatter) | Sets the formatter for column values. Configuration is resolved on first use. | `CsvExporter` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`composeBodyRow()`](#method-composeBodyRow) protected | Collects the rendered values of one data row in column order. | `CsvExporter` |
| [`composeFooterRow()`](#method-composeFooterRow) protected | Collects the footer cell values in column order. | `CsvExporter` |
| [`composeHeaderRow()`](#method-composeHeaderRow) protected | Collects the header cell values in column order. | `CsvExporter` |
| [`createDataColumn()`](#method-createDataColumn) protected | Creates a [`DataColumn`](/docs/extensions/csv-export/reference/datacolumn/index.md) object based on a string in the format of "attribute:format:label". | `CsvExporter` |
| [`initColumns()`](#method-initColumns) protected | Builds a separate schema for this export. | `CsvExporter` |

## Property details

### $batchSize <span id="property-batchSize"></span>

```php
public int $batchSize = 100
```

The number of records to be fetched in each batch.

This property takes effect only in case of [`$query`](/docs/extensions/csv-export/reference/csvexporter/index.md#property-query) usage.

### $columns <span id="property-columns"></span>

```php
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`](/docs/extensions/csv-export/reference/datacolumn/index.md), the "class" element can be omitted.

### $dataProvider <span id="property-dataProvider"></span>

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

The data provider for the view.

This property can be omitted in case [`$query`](/docs/extensions/csv-export/reference/csvexporter/index.md#property-query) is set.

### $emptyCell <span id="property-emptyCell"></span>

```php
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`](/docs/extensions/csv-export/reference/datacolumn/index.md) if a data item is `null`. In that case the [`$nullDisplay`](/docs/extensions/csv-export/reference/csvexporter/index.md#property-nullDisplay)
property will be used to indicate an empty data value.

### $maxEntriesPerFile <span id="property-maxEntriesPerFile"></span>

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

### $query <span id="property-query"></span>

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

The data source query.

Note: this field will be ignored in case [`$dataProvider`](/docs/extensions/csv-export/reference/csvexporter/index.md#property-dataProvider) is set.

## Method details

### export() <span id="method-export"></span>

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

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

**Returns**

[`ExportResult`](/docs/extensions/csv-export/reference/exportresult/index.md): The result that owns the files and handles delivery.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the data source, columns, file limit, writer or result configuration is invalid.

### getFormatter() <span id="method-getFormatter"></span>

```php
public function getFormatter(): yii\i18n\Formatter
```

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

**Returns**

[`yii\i18n\Formatter`](/docs/api/yii/i18n/formatter/index.md): The formatter, cached after the first resolution.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if no formatter can be resolved.

### setFormatter() <span id="method-setFormatter"></span>

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

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$formatter` | `array<string, mixed>\|`[`yii\i18n\Formatter`](/docs/api/yii/i18n/formatter/index.md) | Formatter configuration or instance. |

### composeBodyRow() <span id="method-composeBodyRow"></span>

```php
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`](/docs/extensions/csv-export/reference/column/index.md)`>` | 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() <span id="method-composeFooterRow"></span>

```php
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`](/docs/extensions/csv-export/reference/column/index.md)`>` | Columns of this export. |

**Returns**

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

### composeHeaderRow() <span id="method-composeHeaderRow"></span>

```php
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`](/docs/extensions/csv-export/reference/column/index.md)`>` | Columns of this export. |

**Returns**

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

### createDataColumn() <span id="method-createDataColumn"></span>

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

Creates a [`DataColumn`](/docs/extensions/csv-export/reference/datacolumn/index.md) object based on a string in the format of "attribute:format:label".

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$text` | `string` | The column specification string. |

**Returns**

[`DataColumn`](/docs/extensions/csv-export/reference/datacolumn/index.md): The column instance

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the column specification is invalid.

### initColumns() <span id="method-initColumns"></span>

```php
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`](/docs/extensions/csv-export/reference/column/index.md)`>`: Visible columns bound to this exporter.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a column configuration or an inferred column name is invalid.