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

Creating Excel files

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>                              

View Entire Article

User Comments

Title: great   
Name: Sandy
Date: 2006-11-02 6:33:11 AM
Comment:
Great job.. keep moving ahead
Title: Kar   
Name: Chandrakanta
Date: 2006-11-01 2:56:09 AM
Comment:
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
Comment:
Keep it up.
Title: Nice Article   
Name: Tom Anderson
Date: 2006-10-31 1:46:13 AM
Comment:
Very Nice and informative article for people working on ColdFusion.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 12:25:57 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search