Create Files on the Fly in ColdFusion
page 3 of 6
by Sarita Mishra
Average Rating: 
Views (Total / Last 10 Days): 34264/ 43

Creating Excel files


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">
                        <td>$ Sales</td>
       <td >$245</td>

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
<!--- Set content type. --->
<CFHEADER NAME="Content-Disposition" VALUE="attachment; FILENAME=admin.xls">
<CFCONTENT TYPE="application/vnd.msexcel">
<table bgcolor="blue"cols=4 rows=#Getadmins.recordcount# border="2">
<td>REG ID</td>
<td>USER NAME</td>
<cfloop query="Getadmins">
                <td >#username#</td>

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

View Entire Article

User Comments

Title: great   
Name: Sandy
Date: 2006-11-02 6:33:11 AM
Great job.. keep moving ahead
Title: Kar   
Name: Chandrakanta
Date: 2006-11-01 2:56:09 AM
It is very nice and can also helpful for the new developers. Come across creating different file formats at the time of requirement.
Title: Great Job   
Name: pd
Date: 2006-11-01 2:41:58 AM
Keep it up.
Title: Nice Article   
Name: Tom Anderson
Date: 2006-10-31 1:46:13 AM
Very Nice and informative article for people working on ColdFusion.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-12-09 8:03:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search