Skip to content

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.

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.
ParameterTypeDefaultDescription
pathstring—Path of the output CSV file, including its folder.
dataTableDataTable—The table to write.
ParameterTypeDefaultDescription
delimiterstring","Field separator. Only the first character is used. For a tab, ((char)9).ToString().
includeHeaderbooltrueWrite the header row with the column names.
encodingstring"utf-8"File encoding: utf-8, iso-8859-1, utf-16… See encoding.

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).

MessageCause 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.

Export a query result and email it:

DB Query sql = "SELECT * FROM Customers" → output: customers
Write CSV path = "C:/export/customers.csv" dataTable = customers delimiter = ";" → output: csvPath
SMTP Send ... subject = "Customer export" attachments = arr(csvPath)