Latest Articles:
Committee Members:
Alert Email
Get a short email alert whenever a new entry is published.
Confidential, secure it's piece of cake to keep uptodate.
Reading and Writing CSV files
No matter how complex we wrap up our data, whether its JSON, XML, XLS, databases, etc sometimes you just need to go "old skool" and work with CSV files. Fortunately OpenBD makes that an absolute breeze with two functions designed specifically for the task.
You can take any Query object in CFML and pass it to the ToCSV() function, optionally tagging whether or not you wish the first line to represent the columns, and the delimitor you wish to use.
This will then produce a block of text that you can then store or use as you see fit.
<cfset csvBlock = ToCSV( myquery, true )>
Reading a CSV block is just as painless. Reading a string representing a CSV block will result in a Query object being produced ready for you to use in the normal way. Run QueryOfQuery against it for example.
<cfset csvstring = FileRead( ExpandPath("./my.csv") )>
<cfset myQuery = ReadCSV( csvstring, true )>
Again, you can optionally specify whether the first line of the CSV file will contain the column headers, and the query will be headed up accordingly. ReadCSV() also supports custom delimitors.
There you have it - 2 very easy ways to work with CSV files without any of the hassle.
ps as a bonus function, did you know you can transform your Query object to a clean formatted HTML file using ToHtml()?
Comments (2)
Will that go forward to become part of the CFML standard across all versions?




If other engines wish to implement the functions, then that is completely up to them.
If you feel its useful for other engines, then lobby your engine-vendor and make a request.