Skip to main content
August 2, 2012
Question

Create CSV file from a query?

  • August 2, 2012
  • 1 reply
  • 8978 views

Greetings

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"

file="C:\TEMP\Analysis.csv"

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"

addnewline="yes">

<!--- 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"

file="C:\TEMP\Analysis.csv"

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)#"

addnewline="yes">

</cfloop>

</cfoutput>

<!--- 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

Norman

This topic has been closed for replies.

1 reply

Inspiring
August 2, 2012

You are missing a closing bracket when trimming due_dte.

August 2, 2012

Dan:

Thanks for picking that up- I did put in the missing bracket, but still get the same error.

Inspiring
August 2, 2012

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.