Quickly exporting a table into a CSV
I've just written a few lines of code which easily export the contents of a table into a CSV. This is rather handy if you clients need to download the content locally.
Here is the code:
select *
from applicant
</cfquery>
<cfset csvstr = createObject("java","java.lang.StringBuffer")>
<cfset csvstr.append(qGetApplicants.columnList & "," & Chr(13) & Chr(10) )>
<cfloop query="qGetApplicants">
<cfloop list="#qGetApplicants.columnList#" index="thisColumn">
<!-- remove carrage commas -->
<cfset removedCarageReturns = REReplace(evaluate(thisColumn),'#chr(13)#|\n|\r','','ALL') />
<!-- remove line returns -->
<cfset csvstr.append(replace(removedCarageReturns,",","","all") & ",") />
</cfloop>
<cfset csvstr.append(Chr(13) & Chr(10))>
</cfloop>
<cfheader name="Content-Disposition" value="attachment; filename=ReportApplicantsExport.csv">
<cfcontent type="application/msexcel" variable="#ToBinary( ToBase64(csvstr.toString()) )#" reset="Yes">
Doing it via java make this sooo much quicker!
I have to confess that i picked up some of the java code form a blog - but i cant remember which one so i'm sorry to that person.




