# CsvFile

> CsvFile represents the CSV file.

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

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

CsvFile represents the CSV file.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-cellDelimiter"></span>`$cellDelimiter` | `string` | Delimiter between the CSV file cells. Default: `','`. | `CsvFile` |
| <span id="property-enclosure"></span>`$enclosure` | `string` | The cell content enclosure. Default: `'"'`. | `CsvFile` |
| <span id="property-encoding"></span>`$encoding` | `string` | Output encoding. Input text for UTF-16 export must be UTF-8. Default: `'UTF-8'`. | `CsvFile` |
| <span id="property-entriesCount"></span>`$entriesCount` | `int` | The count of entries written into the file. Default: `0`. | `CsvFile` |
| <span id="property-name"></span>`$name` | `string` | The path of the file. A standalone CSV survives destruction. Default: `''`. | `CsvFile` |
| <span id="property-rowDelimiter"></span>`$rowDelimiter` | `string` | Delimiter between the CSV file rows. Default: `"\r\n"`. | `CsvFile` |
| <span id="property-spreadsheetSafe"></span>`$spreadsheetSafe` | `bool` | Prefixes formula-like text with an apostrophe for spreadsheet import. Default: `false`. | `CsvFile` |
| <span id="property-writeBom"></span>`$writeBom` | `bool\|string\|null` | Writes the encoding's BOM when enabled, or a supplied literal prefix. Default: `false`. | `CsvFile` |
| <span id="property-fileHandler"></span>`$fileHandler` protected | `resource\|null` | Stream for the active write, or `null` when no stream is open. Default: `null`. | `CsvFile` |

## 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/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`__destruct()`](#method-__destruct) | Attempts to finalize an open stream without reporting I/O errors. | `CsvFile` |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/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) |
| [`close()`](#method-close) | Finalizes an open CSV and checks that buffered bytes were flushed. | `CsvFile` |
| [`delete()`](#method-delete) | Removes the named file and ends the writer's lifecycle. | `CsvFile` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/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) |
| [`isComplete()`](#method-isComplete) | Indicates that the finalized file can be included in a result. | `CsvFile` |
| [`open()`](#method-open) | Opens the CSV for writing and allows retry after a failed open. | `CsvFile` |
| [`writeRow()`](#method-writeRow) | Writes a complete CSV record. | `CsvFile` |
| [`composeRowContent()`](#method-composeRowContent) protected | Encodes the cells and prepends a record separator after the first row. | `CsvFile` |
| [`encodeValue()`](#method-encodeValue) protected | Converts supported cell types and applies the selected dialect. | `CsvFile` |
| [`writeContent()`](#method-writeContent) protected | Writes the given content into the file. | `CsvFile` |

## Method details

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

```php
public function __destruct(): mixed
```

Attempts to finalize an open stream without reporting I/O errors.

**Returns**

`mixed`

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

```php
public function close(): bool
```

Finalizes an open CSV and checks that buffered bytes were flushed.

**Returns**

`bool`: Always `true`.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if flushing or closing the file fails.

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

```php
public function delete(): bool
```

Removes the named file and ends the writer's lifecycle.

**Returns**

`bool`: Whether the file is absent after the call.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if flushing or closing the file fails.

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

```php
public function isComplete(): bool
```

Indicates that the finalized file can be included in a result.

**Returns**

`bool`: Whether the file was closed without a failure and is readable.

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

```php
public function open(): bool
```

Opens the CSV for writing and allows retry after a failed open.

**Returns**

`bool`: Always `true`.

**Throws**

- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if the writer was closed or a write failed.
- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the dialect is invalid.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the directory or the file cannot be created.

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

```php
public function writeRow(array<array-key, mixed>|object $rowData): int
```

Writes a complete CSV record.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rowData` | `array<array-key, mixed>\|object` | Cell values: an array, a `Traversable` or public properties. |

**Returns**

`int`: The byte count of the record, without the BOM.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the dialect, a cell value or the text to transcode is invalid.
- [`LogicException`](https://www.php.net/manual/en/class.logicexception.php): if the writer was closed or a write failed.
- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if the file cannot be opened or written.

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

```php
protected function composeRowContent(array<array-key, mixed>|object $rowData): string
```

Encodes the cells and prepends a record separator after the first row.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rowData` | `array<array-key, mixed>\|object` | Cell values: an array, a `Traversable` or public properties. |

**Returns**

`string`: The encoded record.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a cell value cannot be encoded.

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

```php
protected function encodeValue(mixed $value): string
```

Converts supported cell types and applies the selected dialect.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Scalar, `null` or `Stringable` cell value. |

**Returns**

`string`: The cell text, enclosed and escaped when an enclosure is set. An unquoted cell contains a delimiter or line break.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the type is unsupported, the spreadsheet-safe check gets non-UTF-8 text, or

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

```php
protected function writeContent(string $content): int
```

Writes the given content into the file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string` | Content to be written. |

**Returns**

`int`: The number of bytes written.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): on failure.