View comments | RSS feed

Using cfcontent

The cfcontent tag downloads files from the server to the client. You can use this tag to set the MIME type of the content returned by a ColdFusion page and, optionally, define the filename of a file to be downloaded by the current page. By default, ColdFusion returns a MIME content type of text/html so that a web browser renders your template text as a web page.

As with cffile and cfdirectory, you can disable cfcontent processing in the ColdFusion Administrator.

About MIME types

A MIME type is a label that identifies the contents of a file. the browser uses the MIME type specification to determine how to interact with the file. For example, the browser could open a spreadsheet program when it encounters a file identified by its MIME content type as a spreadsheet file.

A MIME content type consists of "type/subtype" format. The following are common MIME content types:

Changing the MIME content type with cfcontent

You use the cfcontent tag to change the MIME content type that returns to the browser along with the content generated from your ColdFusion page.

The cfcontent tag has one required attribute, type, which defines the MIME content type returned by the current page.

To change the MIME content type with cfcontent:

  1. Create an HTML page with the following content:
    <h1>cfcontent_message.htm</h1>
    
    <p>This is a <em>test message</em> written in HTML.</p>
    <p>This is the <em>second paragraph</em> of the test message.  
    As you might expect, it is also written in HTML.</p>
    
  2. Save the file as cfcontent_message.htm in the myapps directory under your web_root.

    This HTML file will be called by the ColdFusion file that you write in steps 3 through 7.

  3. Create a ColdFusion page with the following content:
    <html>
    <head>
    <title>cfcontent Example</title>
    </head>
    
    <body>
    <h3>cfcontent Example</h3>
    
    <cfcontent 
      type = "text/html" 
      file = "C:\CFusionMX\wwwroot\myapps\cfcontent_message.htm" 
      deleteFile = "No">
    </body>
    </html>    
    
  4. If necessary, edit the file = line to point to your myapps directory.
  5. Save the file as cfcontent.cfm in the myapps directory under your web_root and view it in the browser.

    The text of the called file (cfcontent_message.htm) displays as normal HTML, as shown in the following figure:

    Text of the called file in a browser.

  6. In cfcontent.cfm, change type = "text/html" to type = "text/plain".
  7. Save the file and view it in the browser (refresh it if necessary).

    The text displays as unformatted text, in which HTML tags are treated as text:

    Unformatted HTML text.

The following example shows how the cfcontent tag can create an Excel spreadsheet that contains your data.

To create an Excel spreadsheet with cfcontent:

  1. Create a ColdFusion page with the following content:
    <!--- use cfsetting to block output of HTML 
    outside of cfoutput tags --->
    <cfsetting enablecfoutputonly="Yes">
    
    <!--- get employee info --->
    <cfquery name="GetEmps" datasource="CompanyInfo">
      SELECT * FROM Employees
    </cfquery>
    
    <!--- set vars for special chars --->
    <cfset TabChar = Chr(9)>
    <cfset NewLine = Chr(13) & Chr(10)>
    <!--- set content type to invoke Excel --->
    <cfcontent type="application/msexcel">
    
    <!--- suggest default name for XLS file --->
    <!--- use "Content-Disposition" in cfheader for 
    Internet Explorer  --->
    <cfheader name="Content-Disposition" value="filename=Employees.xls">
     <!--- output data using cfloop & cfoutput --->
    <cfloop query="GetEmps">
      <cfoutput>#Employee_ID##TabChar##LastName#
      #TabChar##FirstName##TabChar##Salary##NewLine#</cfoutput>
    </cfloop>
    
  2. Save the file as employees_to_excel.cfm in the myapps directory under your web_root and view it in the browser.

    The data appears in an Excel spreadsheet:

    Data in an Excel spreadsheet.

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


Len said on Aug 8, 2003 at 2:07 PM :
Regarding cfcontent using excel example. I am following the example, but all my output ends up in the first cell. To create an Excel spreadsheet with cfcontent: Create a ColdFusion page with the following content: <!--- use cfsetting to block output of HTML outside of cfoutput tags ---> <cfsetting enablecfoutputonly="Yes"> <!--- get employee info ---> <cfquery name="GetEmps" datasource="CompanyInfo"> SELECT * FROM Employees </cfquery> <!--- set vars for special chars ---> <cfset TabChar = Chr(9)> <cfset NewLine = Chr(13) & Chr(10)> <!--- set content type to invoke Excel ---> <cfcontent type="application/msexcel"> <!--- suggest default name for XLS file ---> <!--- use "Content-Disposition" in cfheader for Internet Explorer ---> <cfheader name="Content-Disposition" value="filename=Employees.xls"> <!--- output data using cfloop & cfoutput ---> <cfloop query="GetEmps"> <cfoutput>#Employee_ID##TabChar##LastName# #TabChar##FirstName##TabChar##Salary##NewLine#</cfoutput> </cfloop> I also used iso-8859-1. Do you have any suggestions. len
Paresh said on Aug 27, 2003 at 1:36 PM :
actually, use TABLE tag and put each data in one separate TD cell.
Paresh said on Aug 27, 2003 at 1:37 PM :
Presently, if you create an Excel file using this method, it creates a single worksheet inside the Excel workbook/file and by default, assigns coldfusion program name to the worksheet.

How can I assign a name of my choice to the worksheet?
halL said on Aug 28, 2003 at 8:19 AM :
There are several errors in the example code, including the one mentioned above plus errors in the database name and one field name.
The following code should generate an Excel spreadsheet multiple rows and cells.

<!--- use cfsetting to block output of HTML outside of cfoutput tags --->
<cfsetting enablecfoutputonly="Yes">

<!--- get employee info --->
<cfquery name="GetEmps" datasource="CompanyInfo">
SELECT * FROM Employee
</cfquery>

<!--- set content type --->
<cfcontent type="application/msexcel">

<!--- suggest default name for XLS file --->
<<!--- "Content-Disposition" in cfheader also ensures
relatively correct Internet Explorer behavior. --->
<cfheader name="Content-Disposition" value="filename=Employees.xls"> --->

<!--- Format data using cfoutput and a table.
Excel converts the table to a spreadsheet.
The cfoutput tags around the table tags force output of the HTML when
using cfsetting enablecfoutputonly="Yes" --->
<cfoutput><table cols="4"></cfoutput>
<cfoutput query="GetEmps">
<tr>
<td>#Emp_ID#</td>
<td>#FirstName#</td>
<td>#LastName#</td>
<td>#Salary#</td>
</tr>
</cfoutput>
<cfoutput></table></cfoutput>
No screen name said on Nov 23, 2003 at 1:30 AM :
How to eport multiple excel sheet (from multiple query record set) in one excel file? Thanks
sense13 said on Jan 23, 2004 at 7:26 AM :
I'm trying to export query results from an UTF-8 oracle database to an excel sheet, as described here.
Everything is fine, even with special or accentuated characters.
The only problem I have is with the Greek language, got only ????? instead of the text.
Any idea/suggestion would be appreciated!
tsippert said on Apr 23, 2004 at 4:27 PM :
sense13,

Sounds like there needs to be a character encoding value added to your MIME type as described in:

http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-a17.htm

example given:
type = "text/html; charset=ISO-8859-1"

try something like:
<cfcontent type="application/msexcel; charset=utf-8">
big_web_monkey said on May 20, 2004 at 10:30 AM :
How do you pass in the type value when you want to serve up different file types. I want to use cfcontent to delieve files to my uses, but cannot restrict the file type. Can you pass the type value in dynamically?
latinode said on May 21, 2004 at 5:39 AM :
I have been looking over the comments above in regards to the use of the cfcontent tag. Can I use this tag to build a document with an extension other than *.html and maintain that document's content properties that comes with the extension used?
velum said on Jul 29, 2004 at 3:37 PM :
I am setting a header for my spreadsheet and it works fine. I was wondering if there is a way to format it. I would like to give it a black background on white text. Here is how I created my header. I have this placed just above my loop.
<cfset delim = ",">
<cfset fileheader = "User Name#delim#Group#delim#Status#delim#Date Assigned#delim#Date Due#delim#Date Completed">
AngelRTorres said on Aug 30, 2004 at 12:54 PM :
It works alright. Problem is that EVERYTHING within the cfoutput tags is saved to the spreadsheet, including the "td" tags.
extdw_doc said on Aug 31, 2004 at 9:54 AM :
LiveDocs is intended for documentation comments and does not receive enough traffic to handle questions like this effectively. I recommend that you post your issue to the online forums: http://webforums.macromedia.com/coldfusion/. If you believe that your issue might result from a ColdFusion bug, you should also submit a bug report on the Macromedia Feature Request/Bug Report Form.
No screen name said on Nov 15, 2004 at 4:08 AM :
But when iam trying to open excel file it is giving error that file name not found in /temporay internet/... like that. But when iam having one instance of excel it is opening without giving any error.please help me out
No screen name said on Nov 27, 2004 at 3:26 PM :
I am running a report which has more than 50,000 rows, When i try to display that in the Excel sheet using Cfcontent, I am having trouble with it, my computer crashes, even I tried with other computer it is the same...
Is there any way to display 50000 rows in the excel sheet..
jrunrandy said on Nov 28, 2004 at 1:26 PM :
Have you tried displaying a 50,000-row spreadsheet in a static XLS file?
pinky122 said on Dec 9, 2004 at 8:06 AM :
this code absolutely works fine ,ie open excel format in .cfm file ,but getting an micro soft excel file cannot acess message...due to work area is already used and cannot be saved ....so please can any ont tell how to solve that problem
naveedkhan said on Dec 13, 2004 at 8:58 AM :
I am having problems with <cfcontent>.
Ex: First time when I run a query I get 10 rows in debug and all these 10 rows are exported to excel and it works fine. Second time when I run a subset of the query with some extra parameters I get 5 rows in debug but the excel still shows up the same previous 10 records instead of 5. That is excel is not refreshing if I close the Excel & open it again.
Does anybody has any idea about it?
acrow said on Dec 16, 2004 at 1:43 PM :
I've been using CFCONTENT type="application/msexcel" with great success until Excel 2003 was loaded on my machine. Now when the page is launch in Excel I see all of the html code. Does anyone know what I need to do differently for Excel 2003???
PhineasGage said on Feb 2, 2005 at 9:54 AM :
I used the discussion above to set up an export page that works fine, with one annoying problem- some of my data are numeric codes with leading zeroes. When the file opens in Excel, the leading zeroes disappear. Anyone know how to force Excel to treat the codes as text, and leave the zeroes? I need something that would act the way the Excel text import wizard does. I've tried enclosing the variable with single or double quotes, which sorta work, except that the quotes show up in Excel along with the zeroes.

Any suggestions?!
RahulM said on Feb 16, 2005 at 12:04 PM :
Excel content display fine on IE but when I try to open the same in safari, it displays html code. Is there a fix for Safari browser on MAC?
Mainak Mamdal said on Jan 13, 2006 at 5:41 AM :
After using <cfcontent> it is better to use <table> tag with <tr> and <td> to have a clear column - row format in the generated Excel sheet
kit001 said on Jan 13, 2006 at 3:28 PM :
Replying PhineasGage 's question on Feb 2, 2005 at 9:54 AM:

Try using &nbsp; instead.
e.g. &nbsp;#leadingZeroNum#
bblain said on Apr 27, 2007 at 7:45 AM :
I realize this is late to the game but still best to post an answer.


The way to get a desired formatting is to create a new blank excel spreadsheet then create a cell with the desired style. Save the spreadsheet as HTML and open that file with a html editing tool. Opening in Microsoft Word does not seem to work for me to edit as it tends to display results instead of code. Excel files converted to HTML also seem overloaded with worthless code and a complete lack of structure. (any MS files for that matter)

Once open browse the file for your CSS format. You can now just add the style to your coldfusion file and assign any cells that style to get the desired affect.

For example:
To display cells with a dollar format I included the style in the head of my cfm file.

.currency
{
mso-number-format:"$\##\,\##\##0\.00";
}

and assigned it to a column

<tr>
<td class="currency">
5
</td>
</tr>

which should effectively display

$5.00

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/manageFiles5.htm