Write CSV
Write CSV
Section titled “Write CSV”Type: write-csv · Package: CSV Activities v1.0.5 · Output: string
Writes a DataTable to a CSV file. Useful to export data in a format any system can import.
How it works
Section titled “How it works”Dumps dataTable into the file at path using the given delimiter. By default it writes the column names as a header row (includeHeader = true). Returns the written path.
- Always overwrites: if the file exists, it is replaced. If the folder does not exist, it is created.
- Automatic quoting: a value containing the delimiter, double quotes or a line break is written in quotes (
"Smith, Ann"), with inner quotes doubled. - Values: each cell is converted to text with the machine’s regional settings (numbers with its decimal separator, dates with its format). To control the format, prepare text columns first (for example with
ToString("yyyy-MM-dd")). Empty cells become empty fields. - Lines: each row ends with a Windows line break (
\r\n). - Encoding: with
utf-8(default) the file gets the BOM, which helps Excel show accented characters correctly.
Required parameters
Section titled “Required parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | Path of the output CSV file, including its folder. |
dataTable | DataTable | — | The table to write. |
Optional parameters
Section titled “Optional parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
delimiter | string | "," | Field separator. Only the first character is used. For a tab, ((char)9).ToString(). |
includeHeader | bool | true | Write the header row with the column names. |
encoding | string | "utf-8" | File encoding: utf-8, iso-8859-1, utf-16… See encoding. |
Output
Section titled “Output”Returns a string with the path of the written file (as you gave it in path). A table with no rows produces a file with just the header (or an empty file with includeHeader = false).
Common errors
Section titled “Common errors”| Message | Cause and fix |
|---|---|
'dataTable' must be a DataTable. | The dataTable expression is not a table (null, a list or another type). Pass the variable holding the DataTable, for example the output of Read CSV or of a query. |
The process cannot access the file '…' because it is being used by another process. | The destination file is open (for example in Excel). Close it before writing. |
Access to the path '…' is denied. | No write permission on the folder. |
Index was outside the bounds of the array. | delimiter is empty. Give a character. |
'path' is required. | The path expression evaluated to null. |
Example
Section titled “Example”Export a query result and email it:
DB Query sql = "SELECT * FROM Customers" → output: customersWrite CSV path = "C:/export/customers.csv" dataTable = customers delimiter = ";" → output: csvPathSMTP Send ... subject = "Customer export" attachments = arr(csvPath)Related activities
Section titled “Related activities”- Read CSV — read a CSV.
- Write Excel Sheet — export to
.xlsx.