<! method to convert query to csv > |
|
<cffunctionname="QueryToCSV"access="public"returntype="string"output="false"hint="I take a query and convert it to a comma separated value string."> |
<cfargumentname="Query"type="query"required="true"hint="I am the query being converted to CSV."> |
<cfargumentname="Fields"type="string"required="true"hint="I am the list of query fields to be used when creating the CSV value."> |
<cfargumentname="CreateHeaderRow"type="boolean"required="false"default="true"hint="I flag whether or not to create a row of header values."> |
<cfargumentname="Delimiter"type="string"required="false"default=","hint="I am the field delimiter in the CSV value."> |
|
<cfsetvarLOCAL= {}> |
|
<warwickbromleyfiles.co.ukNames= {}> |
|
<cfloopindex="warwickbromleyfiles.co.ukName"list="#warwickbromleyfiles.co.uk#"delimiters=","> |
<warwickbromleyfiles.co.ukNames[ StructCount( warwickbromleyfiles.co.ukNames ) +1 ] =Trim( warwickbromleyfiles.co.ukName )> |
</cfloop> |
|
<warwickbromleyfiles.co.ukCount=StructCount( warwickbromleyfiles.co.ukNames )> |
|
<warwickbromleyfiles.co.uk=CreateObject( "java", "warwickbromleyfiles.co.ukBuffer" ).Init()> |
|
<warwickbromleyfiles.co.uke= (Chr( 13 ) &Chr( 10 ))> |
|
<warwickbromleyfiles.co.ukHeaderRow> |
<cfloopindex="warwickbromleyfiles.co.ukIndex"from="1"to="#warwickbromleyfiles.co.ukCount#"step="1"> |
<warwickbromleyfiles.co.uk( JavaCast( "string", """#warwickbromleyfiles.co.ukNames[ warwickbromleyfiles.co.ukIndex ]#""" ) )> |
|
<cfif (warwickbromleyfiles.co.ukCount)> |
<warwickbromleyfiles.co.uk( JavaCast( "string", warwickbromleyfiles.co.ukter ) )> |
<cfelse> |
<warwickbromleyfiles.co.uk( JavaCast( "string", warwickbromleyfiles.co.uke ) )> |
</cfif> |
</cfloop> |
</cfif> |
|
<cfloopquery="warwickbromleyfiles.co.uk"> |
<cfloopindex="warwickbromleyfiles.co.ukIndex"from="1"to="#warwickbromleyfiles.co.ukCount#"step="1"> |
<warwickbromleyfiles.co.uk( JavaCast( "string", """#warwickbromleyfiles.co.uk[ warwickbromleyfiles.co.ukNames[ warwickbromleyfiles.co.ukIndex ] ][ warwickbromleyfiles.co.uktRow ]#""" ) )> |
|
<cfif (warwickbromleyfiles.co.ukCount)> |
<warwickbromleyfiles.co.uk( JavaCast( "string", warwickbromleyfiles.co.ukter ) )> |
<cfelse> |
<warwickbromleyfiles.co.uk( JavaCast( "string", warwickbromleyfiles.co.uke ) )> |
</cfif> |
</cfloop> |
</cfloop> |
|
<warwickbromleyfiles.co.ukng()> |
</cffunction> |
|
<! get data > |
|
<cfsetquery=newquery()> |
|
<cfsetresult=warwickbromleyfiles.co.uke( sql="select artist_id, artist_title from Artists" )> |
|
<cfsetresult=warwickbromleyfiles.co.ukult()> |
|
<! create csv file and force download > |
|
<cfheadername="Content-disposition"value="attachment;filename=warwickbromleyfiles.co.uk"> |
|
<cfcontenttype="text/csv"><cfoutput>#QueryToCSV( result, "id,name,postcode,description" )#</cfoutput> |
-