Whatever message this page gives is out now! Go check it out!

CSVWrite

Last update:
May 18, 2026

Description

Writes a ColdFusion object to a CSV. The object can be a query, array of structs, array of arrays, array of Java arrays, xlsx, csvrecorditerable, and csvString.

History

  • ColdFusion (2025 release): Added the function.

Syntax

CSVWrite(data, inputFormat, destinationFilePath [,writeConfiguration])
Parameters
Name
Required
Type
Description
data
Yes
Object
Query, array of structs, array of arrays, array of Java arrays, xlsx, csvrecorditerable, and csvString from where to retrieve the data.
inputFormat
Yes
String
Type of input object that must be written as a CSV. The possible values are:
  • csvstring: writes the object as a csv string.
  • arrayOfCFArray: writes the object as an array of ColdFusion arrays.
  • arrayofjavaarray: writes the object as an array of Java arrays.
  • arrayofstruct: writes the object as an array of ColdFusion structs.
  • query: writes the object as a ColdFusion query.
  • xlsx: writes the object as a ColdFusion excel object.
  • csvrecorditerable: When reading data from large CSV's (100 MB or more), it’s difficult to return an object that stores all records. Hence, csvrecorditerable is necessary to not store the records and allows various iterable functions to parse the records. These are:
  • next(): iterate over the records.
  • hasNext(): iterate over the records.
  • getObject(): returns the first few elements of the iterable as ColdFusion array. A maximum of 10 records are returned.
  • canGetSize(): returns true or false.
  • close(): call the method if you want to iterate partially over the records.
destinationFilePath
No
String
Path of the file where the CSV must be written.
writeConfiguration
No
Struct
A struct containing the configuration for writing CSV files information. The following values are allowed in the struct:
Key
Value
autoflush
True if you need the file's buffer to be automatically flushed to disk after each write operation.
duplicateHeaderMode
Set the duplicate header names behavior. The values are:
  • ALLOW_ALL
  • ALLOW_EMPTY
  • DISALLOW
delimiter
Set the character delimiter.
escapeCharacter
Set the escape character.
encoding
Encoding to be used for the values in the CSV. The default is UTF-8. The following encoding are supported:
  • utf -8
  • iso-8859-1
  • windows-1252
  • us- ascii
  • shift_jis
  • iso-2022-jp
  • euc -jp
  • euc - kr
  • big5
  • euc - cn
  • utf -16
headerComments
Set the header comments set to the specified values. The comments will be printed first, before the headers.
ignoreHeaderCase
Set the parser case mapping behavior. True if you want to access name/values. False, if you do not want any change. The default is True.
nullString
In CSVwrite , nullString replaces null values with the specified string
quoteCharacter
Set the quote character.
quoteMode
Set the quote mode. The values are:
  • ALL
  • ALL_NON_NULL
  • MINIMAL
  • NON_NUMERIC
  • NONE
readCSVConfig
CSV read configuration options. Add the relevant properties.
recordSeparator
Character to separate the CSV records. For example, comma, /n, etc.
skipHeaderRecord
Ignore the specified number of records/rows at the start of the file. The value must be a Boolean.
trailingDelimiter
True, if you want to add a trailing delimiter. The default is False.
trim
True, if you want to trim leading and trailing blanks. The default is True.
The following matrix lists the support for various output formats:
parameters
ArrayofCFArray
ArrayofJavaArray
xlsx
csvrecorditable
csvString
ArrayOfStruct
query
autoflush=false
Yes
Yes
Yes
Yes
Yes
Yes
Yes
autoflush=true
Yes
Yes
Yes
Yes
Yes
Yes
Yes
delimiter
Yes
Yes
Yes
Yes
Yes
Yes
Yes
duplicateHeaderMode = ALLOW_ALL
Yes
Yes
Yes
Yes
Yes
Yes
Yes
duplicateHeaderMode = ALLOW_EMPTY
Yes
Yes
Yes
Yes
Yes
Yes
Yes
duplicateHeaderMode = DISALLOW
Yes
Yes
Yes
Yes
Yes
Yes
Yes
escapeCharacter
Yes
Yes
Yes
Yes
Yes
Yes
Yes
headerComments with CommentMarker
Yes
Yes
Yes
Yes
Yes
Yes
Yes
nullString
Yes
Yes
NA
Yes
NA
Yes
Yes
quoteCharacter
Yes
Yes
Yes
Yes
Yes
Yes
Yes
QuoteMode=All
Yes
Yes
Yes
Yes
Yes
Yes
Yes
QuoteMode=ALL_Non_Null
Yes
Yes
Yes
Yes
Yes
Yes
Yes
QuoteMode=Minimal
Yes
Yes
Yes
Yes
Yes
Yes
Yes
QuoteMode=Non_Numeric
Yes
Yes
Yes
Yes
Yes
Yes
Yes
QuoteMode=None
Yes
Yes
Yes
Yes
Yes
Yes
Yes
readCSVConfig
NA
NA
NA
NA
Yes
NA
NA
record separator
Yes
Yes
Yes
Yes
Yes
Yes
Yes
skipHeaderRecord=false
Yes
Yes
Yes
Yes
Yes
Yes
Yes
skipHeaderRecord=true
Yes
Yes
Yes
Yes
Yes
Yes
Yes
trailingDelimiter=false
Yes
Yes
Yes
Yes
Yes
Yes
Yes
trailingDelimiter=true
Yes
Yes
Yes
Yes
Yes
Yes
Yes
trim
Yes
Yes
Yes
Yes
Yes
Yes
Yes

Example: Writing a CSV from a query object

<cfquery name="queryResult" datasource="art"> 
    SELECT * 
    FROM ART 
</cfquery> 
<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteQuery.csv"; 
    // write the CSV 
    CSVWrite(queryResult,"query",#theFile#) 
</cfscript>

Example: Writing a CSV from an array of structs

<cfset employees = []> 

 <!--- Create first employee struct ---> 
<cfset employee1 = structNew()> 
<cfset employee1.name = "John Doe"> 
<cfset employee1.age = "30"> 
<cfset employee1.position = "Developer"> 
<cfset arrayAppend(employees, employee1)> 


<!--- Create second employee struct ---> 

<cfset employee2 = structNew()> 
<cfset employee2.name = "Jane Smith"> 
<cfset employee2.age = "25"> 
<cfset employee2.position = "Designer"> 
<cfset arrayAppend(employees, employee2)> 

<!--- Create third employee struct ---> 
<cfset employee3 = structNew()> 
<cfset employee3.name = "Mike Johnson"> 
<cfset employee3.age = "28"> 
<cfset employee3.position = "Manager"> 
<cfset arrayAppend(employees, employee3)> 

<!--- <cfdump var=#employees# > ---> 

<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteAoS.csv"; 
    CSVWrite(employees,"arrayofstruct",#theFile#) 
</cfscript>

Example: Writing a CSV from another CSV as csv string

<cfscript> 
    theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "outputCSVString.csv"; // the csv to write the source csv 
    theFile2=GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; // the source csv 
    value = CSVRead(#theFile2#,"csvstring") // read the source csv 
    CSVWrite(value,"csvstring",#theFile#) // write source csv as csvstring to the destination csv 
</cfscript>

Example: Writing a CSV from an array of structs in ColdFusion

<cfset people = []> 

 <cfset person1 = { 
    "name" = "John", 
    "age" = "30", 
    "city" = "New York" 
}> 

<cfset person2 = { 
    "name" = "Emily", 
    "age" = "25", 
    "city" = "Los Angeles" 
}> 

<cfset arrayAppend(people, person1)> 
<cfset arrayAppend(people, person2)> 

<cfscript> 
     theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "csvWriteArrayOfStructs.csv"; 
    CSVWrite(people,"arrayofstruct",#theFile#) 
</cfscript>

Example: Writing a CSV from from csvrecorditerable

<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteCSVRI.csv"; 
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    writedump(#csvObj#) 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#)  
</cfscript>

Example: Writing a CSV from from csvrecorditerable with delimiter

<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteCSVRIDelimiter.csv"; 
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"delimiter":":"}) 
</cfscript>

More examples

Write a CSV from csvrecorditerable with struct values- {"quoteMode":"ALL","quoteCharacter":"'"}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteCSVRIStruct.csv";      
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"quoteMode":"ALL","quoteCharacter":"'"}) 
</cfscript>
Write a CSV from csvrecorditerable with struct values- {"delimiter":";","quoteMode":"NONE","quoteCharacter":"'","escapeCharacter":"-"}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteCSVRIStruct.csv";      
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"delimiter":";","quoteMode":"NONE","quoteCharacter":"'","escapeCharacter":"-"}) 
</cfscript>
Write a CSV from csvrecorditerable with struct values- {"quoteMode":"MINIMAL","quoteCharacter":"'"}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "customers-100.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteCSVRIStruct.csv";        
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"quoteMode":"MINIMAL","quoteCharacter":"'"}) 
</cfscript>
Write a CSV from csvrecorditerable with struct values- {"header":"#header#","skipHeaderRecord":true}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "TestDataFile.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWrite_pos6_6_1.csv"; 
    header=['2021','Level 1','99999','All industries','Dollars (millions)','H07'] 
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"header":"#header#","skipHeaderRecord":true}) 
</cfscript>
Write a CSV from csvrecorditerable with struct values- {"header":"#header#","skipHeaderRecord":true}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "TestDataFile.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteHeader.csv"; 
    header=['2021','Level 1','99999','All industries','Dollars (millions)','H07'] 
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"header":"#header#","skipHeaderRecord":true}) 
</cfscript>
Write a CSV from csvrecorditerable with struct values- {"header":"#header#","skipHeaderRecord":false}
<cfscript> 
    csvfile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "TestDataFile.csv"; 
    thefile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "spreadsheetCSVWriteHeader.csv"; 
    header=['2021','Level 1','99999','All industries','Dollars (millions)','H07'] 
    csvObj = csvread(#csvfile#,"csvrecorditerable") 
    CSVWrite(csvObj,"csvrecorditerable",#theFile#,{"header":"#header#","skipHeaderRecord":false}) 
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page