Important alert: (current site time 7/16/2013 6:46:28 AM EDT)
 

VB icon

HTML to EXCEL

Email
Submitted on: 6/8/2004 1:56:46 AM
By: Tim Garver  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Cold Fusion 4.5, Cold Fusion 5, Cold Fusion MX
Views: 45541
author picture
(About the author)
 
     Working example of the CFContent tag and how to make it display the current page as an Excel Spread sheet. Uses JavaScript to submit a hidden form. If your report page, like most, is the target of a form post, you will want to replicate any form fields in the hidden form. The hidden form posts to the current page with the DL url variable value of 1. If the current page was passed other URL variables you must also include them in the hidden form action attiribute. Then when you click the Excel Link, the current page will not reload and you will be presented with a download dialog box (sometimes you get two.), choose open and it will be the excel doc. If you like comment, and vote if you found this usefull. Thankx
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :HTML to EXCEL
//**************************************
As is. email if you need help.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: HTML to EXCEL
// Description:Working example of the CFContent tag and how to make it display the current page as an Excel Spread sheet.
Uses JavaScript to submit a hidden form. 
If your report page, like most, is the target of a form post, you will want to replicate any form fields in the hidden form. The hidden form posts to the current page with the DL url variable value of 1. If the current page was passed other URL variables you must also include them in the hidden form action attiribute.
Then when you click the Excel Link, the current page will not reload and you will be presented with a download dialog box (sometimes you get two.), choose open and it will be the excel doc.
If you like comment, and vote if you found this usefull.
Thankx
// By: Tim Garver
//
// Inputs:You must use a valid (simple) HTML table. All included files must have the FULL path (images, style Sheets ect..)
Replace (<) with <
In the cfcontent tag, you can also change this to a word doc type and it will open in word instead of excel. 
//
// Returns:The same file as an excel spread sheet
//
// Assumes:look for the <cfif DL EQ 1> tag. DL is variable i assigned as my flag for downloading as excel. if it is 1, then the page is to reformated so that stuff at the top and bottom dont get included into the spread sheet, like menus and copyright stuff.
//
// Side Effects:Only works with IE 5+ or at least i have never tested it on another browser.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=95&lngWId=9//for details.//**************************************

<!---
<CFQUERY DATASOURCE = "DSN" NAME="QUERY">
SELECTSomething
FROMSomeTable
WHERE(Somecolumn = somevalue)
</CFQUERY>
--->
(<)SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function xL(){
document.X.submit();
}
// End -->
</SCRIPT>
</HEAD>
<!--~========================= END DOCUMENT HEADER =========================~-->
<cfif #DL# EQ 1>
<!-- if DL = 1 we are downloading as excel -->
(<)body bgcolor="#ffffff">
<cfelse>
(<)BODY topmargin=0>
(<)form name="X" action="?DL=1" method="post">
<!--- include any form fields that were posted, here... --->
</form>
</cfif>
(<)a onclick="return xL();">Download as Excel</a>
<TABLE border="1" bgcolor="#ffffff">
<TR ALIGN = "LEFT" valign = "top">
	<TD >Header One</TD>
	<TD >Header Two</TD>
</TR>
<cfloop query="query">
	<CFQUERY DATASOURCE = "DSN" NAME="DETAILS">
	SELECT*
	FROMSomeTable
	WHERE SomeColumn = SomeValue		
	</CFQUERY>
	<cfif DETAILS.RECORDCOUNT GT 0>
		<TR>
			<TD COLSPAN="2" ALIGN="CENTER" bgcolor="F0F0E3">
				<strong><cfoutput>#EMPFNAME# #EMPMI# #EMPLNAME#</cfoutput></strong>
			</TD>
		</TR>
		<cfoutput query="DETAILS">
		<cfif DL EQ 1>
			<tr>
		<Cfelse>
			<tr bgcolor="###IIf(CurrentRow MOD 2, DE('DADAB1'), DE('F0F0E3'))#">
		</cfif><!--- replace with real values --->
				<TD>#columnone#</TD>
				<TD>#columntwo#</TD>
			</TR>	
		</cfoutput>				
	</cfif>
	</cfloop>	
	</TABLE>	
	</TD>
</TR>
</TABLE>
</cfif>
<cfif #DL# EQ 1>
<cfheader 
 name="Content-Type" 
 value="application/msexcel">
<cfheader 
 name="Content-Disposition" 
 value="attachment; filename=SimpleTable.xls">
</cfif>
</BODY>
</HTML>


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/22/2004 2:04:55 PM

Hey, I m kinda new to ColdFusion and I wanna learn the basics first. Could you recommend any free online tutorial site? Thank you.
(If this comment was disrespectful, please report it.)

 
9/9/2004 1:37:12 AM

Sir, I just copied the code and i tried to run it but it displays a static grick and latic page. i did not understand the tags , etc.which are not html tags. will u plese guide me how to do setup to this program so that i can execute it successfully,
I an eagerly waiting for u r reply.
My mail Id: sujangriet@yahoo.com
(If this comment was disrespectful, please report it.)

 
9/9/2004 1:41:07 AM

sir,
Can u please guide me how to run the code ( Html to excel ) . I don't know how to use the tags like , etc.
Please guide me in step wise.
(If this comment was disrespectful, please report it.)

 
9/10/2004 12:25:16 PMTim Garver

Yes,
I sent you an email. also you should look into setup and administration of cold fusion if you are seting up your own box. if you are using a hosted solution, then you must use a cold fusion server to run any CF code.

Also you may want to buy a book on the basics of cold fusion.

Hope that helps
(If this comment was disrespectful, please report it.)

 
9/10/2004 12:28:07 PMTim Garver

http://livedocs.macromedia.com/coldfusion/6.1/index.html
here is the macromedia livedocs site which has all the latest documentation on Cold Fusion
(If this comment was disrespectful, please report it.)

 
10/11/2004 3:13:35 AM

Pretty Useful ,
But Browser Dependant
(If this comment was disrespectful, please report it.)

 
10/13/2004 12:26:59 PMTim Garver

Just an update.
You can use the tag:
"" and rap that around your output section, then use cffile to write the file on the web server instead of using the cfcontent tags to force it to download.
something like
(<)cfset myfilename ="myfile.xls">
(<)cfsavecontent variable="myfile">
....content to create
(
(<)cffile action="write" file="d:\#myfilename#" content="#myfile#">
then dont use the cfheader tags. and make a link to the newly created file.
Then it will not be browser dependant
(If this comment was disrespectful, please report it.)

 
11/2/2004 12:54:21 AM

I have name="Content-Type"
value="application/msexcel">
name="Content-Disposition"
value="attachment; filename=filename.xls">
being called in a .cfm which is displayed in Frame[2] of a frameset.This .cfm is called from a dropdown based on value selected from dropdown in Frame[1].But once I click view in Excel and finish saving a .xls file I am not able to get the results in the browser for the selected menu option. I need to close the browser and reopen it again to select any other menu option from dropdown in Frame[1].Could you please help me out in this..

(If this comment was disrespectful, please report it.)

 
11/2/2004 8:53:06 AMTim Garver

Sounds like you have some issues with the dropdowns and the frames. simplify your program take out the frames and drop downs and test it that way. also take the
(If this comment was disrespectful, please report it.)

 
11/2/2004 9:15:46 AMTim Garver

send me your code ill take a look at it. tim@timgarver.com
(If this comment was disrespectful, please report it.)

 
11/9/2004 10:32:44 AM

The code sample was easy to use, and worked well. I did encounter one problem. The external CSS that is referenced to format the excel file does not stay linked to the page. Coldfusion complains that my .css file can't be found. I suppose I could hard code the style sheet in. Is there a better way?
(If this comment was disrespectful, please report it.)

 
11/9/2004 11:10:35 AMTim Garver

If you want CSS files or images in the spread sheet, you must include the FULL path to the file.

Tim
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.