Create data export to Excel or to CSV Format
Fast and secure method to export data from Database to Excel using Coldfusion cftable CFTAG.
Try this simple code
<cfsavecontent variable="to_excel">
<!--- save all data static and dinamic into variable to_excel --->
<HTML>
<HEADER>
<title>CUSTOMERS DATA</title>
</HEADER>
<body leftmargin="0" topmargin="0" rightmargin="0"
bottommargin="0" marginwidth="0" marginheight="0">
<cftable query = "REPORT_HOTEL_CONTRATTI" startRow = "1"
colSpacing = "3" COLHEADERS HTMLTable border="Yes">
<cfcol header = "<b>KEY</b>" align = "Left" width = 15 text = "#CUSTOMER_KEY#">
<cfcol header = "<b>CATEGORY</b>" align = "Left" width = 30 text = "#CATEGORY#">
<cfcol header = "<b>REGION</b>" align = "Left" width = 30 text = "#UCASE(REGION)#">
<cfcol header = "<b>CITY</b>" align = "Left" width = 20 text = "#UCASE(CITY)#">
<cfcol header = "<b>ONLINE</b>" align = "Left" width = 20 text = "#dateformat(DATAPUB, 'dd/mm/yy')#">
<cfcol header = "<b>ON/OFF</b>" align = "Left" width = 20 text = "<cfif val(CUST_ONOFF) is 1>ON<cfelse>OFF</cfif>">
</cftable>
</body>
</html>
</cfsavecontent>
<cfprocessingdirective suppresswhitespace="yes">
<cfsetting enablecfoutputonly="yes" showdebugoutput="no">
<cfcontent type="application/unknown">
<cfheader name="Content-Disposition" value="attachment; filename=to_excel.xls">
<cfoutput>#to_excel#</cfoutput>
</cfprocessingdirective>
<!--- save all data static and dinamic into variable to_excel --->
<HTML>
<HEADER>
<title>CUSTOMERS DATA</title>
</HEADER>
<body leftmargin="0" topmargin="0" rightmargin="0"
bottommargin="0" marginwidth="0" marginheight="0">
<cftable query = "REPORT_HOTEL_CONTRATTI" startRow = "1"
colSpacing = "3" COLHEADERS HTMLTable border="Yes">
<cfcol header = "<b>KEY</b>" align = "Left" width = 15 text = "#CUSTOMER_KEY#">
<cfcol header = "<b>CATEGORY</b>" align = "Left" width = 30 text = "#CATEGORY#">
<cfcol header = "<b>REGION</b>" align = "Left" width = 30 text = "#UCASE(REGION)#">
<cfcol header = "<b>CITY</b>" align = "Left" width = 20 text = "#UCASE(CITY)#">
<cfcol header = "<b>ONLINE</b>" align = "Left" width = 20 text = "#dateformat(DATAPUB, 'dd/mm/yy')#">
<cfcol header = "<b>ON/OFF</b>" align = "Left" width = 20 text = "<cfif val(CUST_ONOFF) is 1>ON<cfelse>OFF</cfif>">
</cftable>
</body>
</html>
</cfsavecontent>
<cfprocessingdirective suppresswhitespace="yes">
<cfsetting enablecfoutputonly="yes" showdebugoutput="no">
<cfcontent type="application/unknown">
<cfheader name="Content-Disposition" value="attachment; filename=to_excel.xls">
<cfoutput>#to_excel#</cfoutput>
</cfprocessingdirective>