---
url: /process-functions/csv-writer.md
description: Writes rows to a CSV file, one array of values per row.
---

# &#x20;CSVWriter

Writes rows to a CSV file, one array of values per row.

::: code-group

```js [export.js]
const writer = datasource.csv("exports/actuals.csv");

writer.write(["year", "month", "product", "amount"]);
writer.write(["2026", "Jan", "Widgets", 1200.5]);
writer.write(["2026", "Jan", "Gadgets", 830]);

writer.close();
```

```csv [actuals.csv]
"year","month","product","amount"
"2026","Jan","Widgets",1200.5
"2026","Jan","Gadgets",830
```

:::

## Constructor

### datasource.csv `datasource.csv(fileName)`  {#datasource-csv}

Creates a CSV file in the filesystem and returns a `CSVWriter`, or `null` when the path is invalid. If the file already exists, it's replaced.

| Parameter | Type | Description |
|---|---|---|
| `fileName` | `string` | The path of the file, for example `"exports/actuals.csv"`. Missing folders are created. |

```js
// A file in the exports folder
const writer = datasource.csv("exports/actuals.csv");

// A file in a folder that doesn't exist yet; the folder is created
const writer = datasource.csv("exports/2026/actuals.csv");
```

## Overview

* **One row per call:** pass [write()](#write) an array of values, one per column.
* **How values are written:** numbers and booleans are written bare, `null` is written as `NULL`, and everything else is written as quoted text.
* **Format:** rows are comma-delimited, text is wrapped in double quotes, and rows are separated by a Windows-style line break (`\r\n`). Change these with the setter methods **before writing any rows**.
* **Always close:** call [close()](#close) when you've finished, so everything is written to the file.

## Methods

### write `write(values)`  {#write}

Writes one row to the file. Returns `true` on success, or `false` when the file is closed or `values` isn't an array.

| Parameter | Type | Description |
|---|---|---|
| `values` | `array` | The values for one row, in column order. |

### close `close()` {#close}

Writes any buffered rows to the file and closes it. Always call it when you've finished writing: until then, the file may not be complete. Writes after `close()` are ignored and logged as errors.

### setDelimiter `setDelimiter(delimiter)` {#setdelimiter}

Sets the text written between values. The default is a comma. Call it before writing any rows.

| Parameter | Type | Description |
|---|---|---|
| `delimiter` | `string` | The delimiter, for example `";"`, or `"\t"` for tab. |

### getDelimiter `getDelimiter()`  {#getdelimiter}

Returns the text written between values, `","` unless changed with `setDelimiter()`.

### setQuoteCharacter `setQuoteCharacter(quoteCharacter)` {#setquotecharacter}

Sets the character wrapped around text values. The default is a double quote. Call it before writing any rows.

| Parameter | Type | Description |
|---|---|---|
| `quoteCharacter` | `string` | The quote character. Pass `""` to turn quoting off. |

### getQuoteCharacter `getQuoteCharacter()`  {#getquotecharacter}

Returns the character wrapped around text values, `"` unless changed with `setQuoteCharacter()`.

### setLineBreak `setLineBreak(lineBreak)` {#setlinebreak}

Sets the text written between rows. The default is `"\r\n"`. Call it before writing any rows.

| Parameter | Type | Description |
|---|---|---|
| `lineBreak` | `string` | The line break, for example `"\n"` for Unix-style line endings. |

### getLineBreak `getLineBreak()`  {#getlinebreak}

Returns the text written between rows, `"\r\n"` unless changed with `setLineBreak()`.

## Examples

### A semicolon-delimited file

::: code-group

```js [export.js]
const writer = datasource.csv("exports/budget.csv");
writer.setDelimiter(";");

writer.write(["account", "month", "amount"]);
writer.write(["4000", "Jan", 1000]);
writer.write(["4000", "Feb", 1100]);

writer.close();
```

```csv [budget.csv]
"account";"month";"amount"
"4000";"Jan";1000
"4000";"Feb";1100
```

:::

### Values without quotes

::: code-group

```js [export.js]
const writer = datasource.csv("exports/products.csv");
writer.setQuoteCharacter("");

writer.write(["code", "name", "active"]);
writer.write(["W-01", "Widget", true]);
writer.write(["G-02", "Gadget", false]);

writer.close();
```

```csv [products.csv]
code,name,active
W-01,Widget,true
G-02,Gadget,false
```

:::

## Related

* [CSVReader](/process-functions/csv-reader): reads CSV files.
