Quick and dirty CSV export

Do you always find that you are asked to drop export buttons next to recordsets all the time? We are, all the time. Almost to the point that it is mandatory for us to put an export button in every time we show a recordset over 25 rows.

We wanted an easy way to dump the recordset to a CSV file without too much fussing around so we came up with this little snippet of code that takes care of all the fuss. All you need is our CSV library and a submit button away you go

<!--- Some code that generates a recordset (called qRecordset) --->
<!--- Are we exporting? --->
<cfif structKeyExists(form,"export")>
   <cfset filename="MyCoolExport.csv">

   <cfset oCSV = createObject("component","net.redbd.cfc.csv")>
   <cfset csvExport = oCSV.query2CSV(qRecordset)>
   <cfsetting enablecfoutputonly="true" showdebugoutput="false">
   <cfcontent reset="Yes" type="application/csv">
   <cfheader name="Content-Disposition" value="attachment; filename=#filename#">
   <cfoutput>#csvExport#</cfoutput>
   <cfabort><!--- We don't want any footers etc --->
</cfif>

You will notice that we are looking for the existence of form.export and if we find it, we stop executing the page and start dumping out the CSV file.

If you want extra credit from your users, you can even give your users friendly column names, just pass another two argument when you generate the CSV. They are two lists of colum names, the first are the names used in your recordset the second are the names that you want to appear in your CSV file. Obviously order is important here so make sure you get them matched up

<cfset csvExport = oCSV.query2CSV(qRecordset,
                           "ID,NAME,ACCOUNTMANAGER,COUNTRYCODE,DTLASTUPDATED",
                           "ID,Account Name,Account Manager,Country,Last Updated")>

And that is it, Hopefully this will save you a few minutes this Friday and get you to the pub that little bit sooner.

Cheers,
Lucas.

Related Blog Entries

Comments
Ethan Cane's Gravatar Thanks for this Lucas,

Really helpful to find snippets like this and to know that resources like this one exist out there.

Hope to hear you speak again soon at UKCFUG.

Ethan
# Posted By Ethan Cane | 8/5/06 5:53 AM
Lucas Sherwood's Gravatar Hey Ethan,
glad to hear you are finding it useful!

Since I am in AU now, i think you are going to have to make the trek to WebDU to hear me speak again
# Posted By Lucas Sherwood | 8/7/06 6:28 AM
Greg Embry's Gravatar Ok, I'm beating my head on the wall. I am attempting to use the query2csv with Japanese output and get the dreaded ????'s. Not sure what's going on with the encoding. Any ideas you can throw my way? Thanks for the usefull cfc's.
# Posted By Greg Embry | 12/29/06 12:50 PM
tony's Gravatar If you want extra credit from your users, you can even give your users friendly column names, just pass another two argument when you generate the CSV. They are two lists of colum names, the first are the names used in your recordset the second are http://www.batteryfast.com/acer/travelmate-2150.ht... acer travelmate 2150 battery,
http://www.batteryfast.com/acer/travelmate-4150.ht... acer travelmate 4150 battery,
http://www.batteryfast.com/acer/travelmate-2350.ht... acer travelmate 2350 battery,
http://www.batteryfast.com/acer/btp-36d1.htm acer btp-36d1 battery,
http://www.batteryfast.com/acer/travelmate-350.htm... acer travelmate 350 battery,
http://www.batteryfast.com/acer/travelmate-352.htm... acer travelmate 352 battery,
http://www.batteryfast.com/acer/travelmate-353.htm... acer travelmate 353 battery,
http://www.batteryfast.com/acer/travelmate-354.htm... acer travelmate 354 battery,
http://www.batteryfast.com/acer/btp-39d1.htm acer btp-39d1 battery,

http://www.batteryfast.com/acer/btp-620.htm acer btp-620 battery,
http://www.batteryfast.com/acer/travelmate-620.htm... acer travelmate 620 battery,
http://www.batteryfast.com/acer/travelmate-630.htm... acer travelmate 630 battery,
http://www.batteryfast.com/acer/btp-43d1.htm acer btp-43d1 battery,
http://www.batteryfast.com/acer/travelmate-220.htm... acer travelmate 220 battery,
http://www.batteryfast.com/acer/travelmate-280.htm... acer travelmate 280 battery,
http://www.batteryfast.com/acer/squ-207.htm acer squ-207 battery,
http://www.batteryfast.com/acer/aspire-1700.htm acer aspire 1700 battery,
http://www.batteryfast.com/acer/squ-302.htm acer squ-302 battery,

http://www.batteryfast.com/acer/aspire-1350.htm acer aspire 1350 battery,
http://www.batteryfast.com/acer/aspire-1510.htm acer aspire 1510 battery,
http://www.batteryfast.com/asus/a4000.htm asus a4000 battery,
http://www.batteryfast.com/asus/a42-a4.htm asus a42-a4 battery,
http://www.batteryfast.com/asus/a4.htm asus a4 battery,
http://www.batteryfast.com/asus/a32-f3.htm asus a32-f3 battery,
http://www.batteryfast.com/compaq/n400.htm compaq n400 battery,
http://www.batteryfast.com/compaq/n400c.htm compaq n400c battery,
http://www.batteryfast.com/compaq/n410c.htm compaq n410c battery,
the names that you want to appear in your CSV file. Obviously order is important here so make sure you get them matched up
# Posted By tony | 9/26/08 3:10 PM