-
-
Notifications
You must be signed in to change notification settings - Fork 36
writeCsv
Generates a CSV string or file from a data object.
writeCsv().fromData( data )[...optional configuration calls].execute();
By default the method returns an object to which further optional calls can be chained before finally calling execute()
to generate the CSV string/file.
Chainable? No.
Use the .fromData()
chained method to pass in the data you wish to convert to CSV. This can be:
- A query object
- An array of arrays (1 per row)
- An array of structs (1 per row)
data = [ [ "first", "last" ], [ "Frumpo", "McNugget" ] ];
csv = spreadsheet.writeCsv()
.fromData( data )
.execute();
Use .withQueryColumnsAsHeader()
to include the column names as the first row of the CSV.
data = QueryNew( "first,last", "VarChar,VarChar", [ [ "Frumpo", "McNugget" ] ] );
csv = spreadsheet.writeCsv()
.fromData( data )
.withQueryColumnsAsHeader()
.execute();
Use .withStructKeysAsHeader()
to include the row struct keys as the first row of the CSV.
data = [ { first: "Frumpo", last: "McNugget" } ];
csv = spreadsheet.writeCsv()
.fromData( data )
.withStructKeysAsHeader()
.execute();
You can write your data directly to a CSV file.
data = QueryNew( "first,last", "VarChar,VarChar", [ [ "Frumpo", "McNugget" ] ] );
spreadsheet.writeCsv()
.fromData( data )
.toFile( pathToYourFile )
.withQueryColumnsAsHeader()
.execute();
Predefined Formats are preset Commons CSV configuration combinations optimized for for different contexts, such as tab-delimited data.
data = [ [ "first", "last" ], [ "Frumpo", "McNugget" ] ];
csv= spreadsheet.writeCsv()
.fromData( data )
.withPredefinedFormat( "TDF" )
.execute();
If not specified the EXCEL predefined format will be used.
Whether to allow missing column names in the header line. Default: true
. Commons CSV documentation
Whether to flush on close. Default: true
. Commons CSV documentation
Sets the comment start marker to the specified character. Commons CSV documentation
Sets the delimiter character. Commons CSV documentation
To set tab as the delimiter you can use any of the following values as parameters: "#Chr( 9 )#"
, "\t"
, "tab"
, "TAB"
Sets the duplicate header names behavior. Possible values: "ALLOW_ALL", "ALLOW_EMPTY", "DISALLOW". Commons CSV documentation
Sets the escape character. Commons CSV documentation
Manually sets the row values which will be detected as the header. To auto-detect the header from the first row, use withFirstRowIsHeader()
(see above) Commons CSV documentation
Sets the header comments set to the given values. The comments will be printed first, before the headers. Commons CSV documentation
Sets the empty line skipping behavior, true to ignore the empty lines between the records, false to translate empty lines to empty records. Default: true
. Commons CSV documentation
Sets the quote character. Commons CSV documentation
Sets the quote mode. Possible values: "ALL", "ALL_NON_NULL", "MINIMAL", "NON_NUMERIC", "NONE". Commons CSV documentation
Sets whether to add a trailing delimiter. Default: true
. Commons CSV documentation
Sets whether to trim leading and trailing blanks. Default: true
. Commons CSV documentation
If your CFML engine supports parallel loop processing (Lucee 5+, ACF2021+) you can specify how many threads you would like to use to construct the CSV in parallel. WARNING: Using this option can have unexpected results such as rows out of order or system crashes. Use with care.
spreadsheet.writeCsv()
.fromData( mybigdata )
.toFile( myfilepath )
.withParallelThreads( 4 )
.execute();