For downloading ?
Main Topics
Browse All TopicsIs there a way to export on the fly a csv file created from a sql query with a click of a button using cfm?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is an example of writing a csv file that has client info to a file on the server:
<!--- start the file by writing the first line --->
<cffile
action="WRITE"
addnewline="Yes"
file="myquery.csv"
output="contact,client,str
<!--- loop through the query, writing each row to a new line in the file --->
<cfoutput query="getNames">
<cfset FullName = getNames.FirstName & ' ' & getNames.LastName>
<cffile
action="APPEND"
addnewline="Yes"
file="myquery.csv"
output="""#FullName#"",""#
</cfoutput>
If you wanted to then provide a link to the user to that file, you could then add:
<a href="http://mysite.com/my
or, you could redirect the user after the file is created (the user's browser would determine whether it displayed on their screen or if the user is prompted to save or open):
<cflocation url="http://mysite.com/myq
To download a CSV file (without writing a file to the server). You could use the CSVFormat() function from cflib.org. It converts a query to to a csv formatted string.
http://www.cflib.org/udf.c
The code for the download would be:
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="text/csv">
<cfoutput>#CSVFormat(mytes
Or to have the CSV open in Excel:
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="application/vnd.ms-
<cfoutput>#CSVFormat(mytes
Also, is there a way to not store it on the server but immediately pull up the Save/Cancel dialog so the user can save it to their machine. If it must be saved to the server, then how about if the user clicks Save in the dialog box, then saves it, then the file on the server gets deleted. Any ideas?
@COwebmaster,
My suggestion doesn't write a file to the server at all.
Using CFFILE is also good. But its better to write to the file once, intstead of appending each line, to avoid a lot of expensive IO operations.
<cfset theFileContents = "ColumnHeader1,ColumnHeade
<cfloop query="yourQuery">
<cfset theFileContents = theFileContents & "#Column1#,#Column2##NewLi
</cfloop>
<cffile action="WRITE" output="#theFileContents#"
aqx , ok then how can I tie in your suggestion above with the download?
The code for the download would be:
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="text/csv">
<cfoutput>#CSVFormat(mytes
Or to have the CSV open in Excel:
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="application/vnd.ms-
<cfoutput>#CSVFormat(mytes
COwebmaster,
A full example would be someting like this
<cfquery nme="YourQuery" datasource="abc">
SELECT Column1, Column2, Column2 FROM YourTable
</cfquery>
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="text/csv">
<cfoutput>#CSVFormat(YourQ
Ok, so using this example will:
1.) create the csv file
2.) Open up a Save/Cancel dialog box
Correct?
<cfquery nme="YourQuery" datasource="abc">
SELECT Column1, Column2, Column2 FROM YourTable
</cfquery>
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="text/csv">
<cfoutput>#CSVFormat(YourQ
@COwebmaster
Correction, it will do that IF you've installed the UDF correctly :)
http://www.cflib.org/udf.c
Its just a custom function. You can put them anywhere you want. For example, you could copy the code from cflib.org and save it to a file named "myFunctions.cfm" in the same diretory as your download page.
<!---- Save the COMPLETE code to file named "myFunctions.cfm" ---->
function CSVFormat(query)
{
.....
}
Then change your download page to include the function like this
<!---- Will include the function code --->
<CFINCLUDE TEMPLATE="myFunctions.cfm"
<cfquery nme="YourQuery" datasource="abc">
SELECT Column1, Column2, Column2 FROM YourTable
</cfquery>
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="text/csv">
<cfoutput>#CSVFormat(YourQ
Ok, I tried but I get an error saying:
A tag starting with 'CF' has been detected. This tag is not supported by this version of ColdFusion. Please verify your typo and try again.
Unknown tag: CFCONTENTTYPE.
ColdFusion cannot determine how to process the tag CFCONTENTTYPE because the tag is unknown and not in any imported tag libraries. The tag name might be misspelled.
To your response:
"Btw, the basic concept of converting the query to CSV format is the exactly same as what theamzngq described. The UDF just does the looping for you. The only real difference is whether you want to create a physical file on the server or not. "
- I just want the user to download the csv not display it in the browser.
By using content-disposition "attachment" (not "inline) it should prompt to download
<CFHEADER NAME="Content-Disposition"
But you could also try using an XLS extension:
<CFHEADER NAME="Content-Disposition"
<CFCONTENTTYPE VALUE="application/vnd.ms-
The blank is probably caused by NOT putting cfoutput on the same line, like this:
<cfoutput>#CSVFormat(mytes
The simplest way to change the headers is to alias the column names in your sql query.
SELECT Column1 AS [TheFirstColumn], Column2 AS [TheSecondColumn] ....
Or you could change the function so it does not append th column headers, and append the header names you want manually.
Business Accounts
Answer for Membership
by: theamzngqPosted on 2007-06-08 at 09:31:08ID: 19243478
Where does the file need to end up? In a folder on the web server, or does the user need a prompt to save/download it?