Skip navigation
Currently Being Moderated

Create CSV file from a query?

Aug 2, 2012 6:44 AM



I am attempting to create a csv file from a query that would pop up for downloading.


I found this code:


<!--- Create the file with column headers. It is crucial that you have all the outputted text on one line with no spaces between the comma and the next column header.--->


<cffile action="write"


output="Assigned Technician,Received Date,Received Time,Priority,Due Date,User Name,User Office,Assigned Date,Assigned Time,Completed Date,Completed Time,Hours to Complete,Description"



<!--- Output the contents of the qryResults query ---> <cfoutput> <cfloop query="qryResults">


<!--- Append (Insert) the contents of the query into the already created CSV file. Make sure that you keep all the output on one line exactly like the column header--->


<cffile action="append"


output= "#(tech_lname)#, #TRIM(queue_dte)#, #TRIM(queue_tme)#,  #TRIM(queue_priority)#, #TRIM(due_dte#, #TRIM(user_name)#,  #TRIM(office_name)#, #TRIM(assign_date)#, #TRIM(assign_time)#, #TRIM(repair_dte)#, #TRIM(repair_time)#, # TRIM(repair_ttc)#, #Left(q_description,128)#"






<!--- This will pop up the file for downloading. You can open it in excel or any other text editing software. --->


<cflocation url="#your.url#/Analysis.csv">





Something is wrong with the query.



Here's the error:


Invalid CFML construct found on line 135 at column 105. 

ColdFusion was looking at the following text:



The CFML compiler was processing:


An expression beginning with TRIM, on line 135, etc.

135 : output= "#(tech_lname)#


Any help would be appreciated....


Thanks in advance



  • Currently Being Moderated
    Aug 2, 2012 7:11 AM   in reply to sakonnetweb

    You are missing a closing bracket when trimming due_dte.

    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 7:50 AM   in reply to sakonnetweb

    Given that the error is with your first field, it might have something to do with the brackets around the variable name.


    By the way, did you know that cfoutput has a query attribute that makes this code redundant?

    <cfoutput> <cfloop query="qryResults">


    Also, your code will probably run faster if you use cfsavecontent to create a variable for your file contents.  Then you only have to write to it once.

    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 10:05 AM   in reply to sakonnetweb

    You can always use cfcontent to make the file available.

    Mark as:
  • Currently Being Moderated
    Aug 2, 2012 12:14 PM   in reply to sakonnetweb

    You can't store anything on the user's machine in any directory.  You can only store files on your application server.  For many developers, these are the same machine when they are writing code.  It sounds like you are one of these developers.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points