Thursday, July 5, 2007

How to Output Query In Excel Format Using Coldfusion MX

Couple of Projects that I am working on required to download the data from database in Excel fromat, so that the client can download it on his local system.
One of the efficient way I have found is using Query2Excel Tag which is available FREE on internet. You can download if from www.cflib.org
Below code is just how to call that function, for excel download.

<!--- Query to get information from database --->

<cfquery name="get_info" datasource="#whatever#">

select name1, name2, name3 from yourdatabase

</cfquery>

<!--- give the column names and headers for Excel file --->

<cfscript>

cols = "name1, name2, name3";

heads = "Name1, Name2, Name3"

</cfscript>

<!--- use CFHEADER tag --->

<cfheader name="Content-Disposition" value="inline; filename=data.xls">

<cfcontent type="application/msexcel">

<!--- output the results using the query2excel function --->

<cfoutput> #Query2Excel(get_info, heads, cols)# </cfoutput>

<!--- End of code --->

That's all you need :-)

No comments: