Steps
1) Create a .CFM page in ColdFusion and include some HTML
codes. Then the browser will load those html codes as an Excel spreadsheet.
Example: Creating Excel Sheet using HTML tag
Listing 2
<CFHEADER NAME="Content-Disposition" VALUE="inline; FILENAME=test.xls">
<CFCONTENT TYPE="application/vnd.msexcel">
<table border="2">
<tr>
<td>Month</td>
<td>Quantity</td>
<td>$ Sales</td>
</tr>
<tr>
<td>January</td>
<td>80</td>
<td >$245</td>
</tr>
</table>
The above html table will be treated as an Excel worksheet
and will be shown to the user in a different browser, instead of in the same
application. The "Content-Disposition" value="inline tag does
this work.
Note: We can use CFQUERY tag to retrieve data from database
and save them as an .xls file as well.
Example: Creating Excel Sheet using Query Result
Listing 3
<!--- Get Admin info. --->
<cfquery name="GetAdmins" datasource="testReport">
SELECT * FROM tb_company_admin
</cfquery>
<!--- Set content type. --->
<CFHEADER NAME="Content-Disposition" VALUE="attachment; FILENAME=admin.xls">
<CFCONTENT TYPE="application/vnd.msexcel">
<cfoutput>
<table bgcolor="blue"cols=4 rows=#Getadmins.recordcount# border="2">
<tr
<td>ID</td>
<td>REG ID</td>
<td>USER NAME</td>
</font>
</tr>
<cfloop query="Getadmins">
<tr>
<td>#admin_ID#</td>
<td>#reg_id#</td>
<td >#username#</td>
</tr>
</cfloop>
</table>
</cfoutput>
Example: Creating Excel Sheet using coldfusion <cftable>
tag for the above query
Listing 4
<cftable query = "Getadmins"startRow = "1" colSpacing = "3"
color="oxffffff" HTMLTable colheaders>
<!--- Each cfcol tag sets the width of a column in the table,
the header information, and the text/CFML for the cell. --->
<cfcol header = "<b>Admin ID</b>"
align = "Left"
width = 2
text = "#admin_ID#">
<cfcol header = "<b>Comapny ID</b>"
align = "Left"
width = 15
text = "#reg_id#">
<cfcol header = "<b>User Name</b>"
align = "Center"
width = 15
text = "#username#">
</cftable>