Skip to main content
Known Participant
April 28, 2008
Question

exporting data to a comma dilimeted file

  • April 28, 2008
  • 2 replies
  • 374 views
looking for a way with coldfusion to take data and export them to a comma delimited file?
    This topic has been closed for replies.

    2 replies

    Inspiring
    April 28, 2008
    Did you try using cffile?
    Participating Frequently
    April 28, 2008
    How about something like the following. This example assumes MS SQL, as other databases may use a different concatenation character than +, but you should get the idea (Oracle uses ||, etc.).

    <!---Concatenate the fields in your query into a single CSV "string" per row --->
    <cfquery name="your_query" datasource="your_dsn">
    SELECT field1+','+
    field2+','+
    field3+','+
    field4 AS your_data
    FROM your_table
    WHERE whatever
    </cfquery>

    <!---Add CR and LF to each row returned--->
    <cfset query_content=''>
    <cfoutput query="your_query">
    <cfset "query_content" = query_content & #your_data# & CHR(13) & CHR(10)>
    </cfoutput>

    <!---Write CSV to file--->
    <cffile ACTION="Write"
    FILE=#expandpath(".\your_directory\your_filename.csv")#
    OUTPUT="#query_content#">

    Phil
    Inspiring
    April 28, 2008
    Brian Wright_ wrote:
    > looking for a way with coldfusion to take data and export them to a comma delimited file?

    Rather straight forward, create comma delimited data, then write or
    deliver it.

    <cfsavecontent variable="foobar">
    Here,is,some,comma,delimited,data
    The,quick,brown,fox,jumped,over,the,lazy,dog
    </cfsavecontent>

    <cffile action="write" file="..." output="#foobar#">

    OR

    <cfheader name="Content-Disposition"
    value="attachment;filename=thefile.csv">

    <cfcontent reset="yes"><cfoutput>#foobar#</cfoutput>