We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Need help exporting Coldfusion to Excel?

Medium Priority
3,010 Views
Last Modified: 2013-12-16
I've been going thru the EE solutions on how to export my Coldfusion table(query output) to MS Excel 2007. I have most of it working but need some help to finish.

 What Im trying to do is add a download button to my page so when it is clicked the info on that page is downloaded as an Excel document.

When I click the download button it prompts to save or open the document "myfile.xls". When I open the file I get the following:

1. I get a message that myfile.xls, is a different format than specified file extension. Would you like to open anyway.

2. I get another message after I click ok, that  its missing  default.css file. But the colors and formatting is exactly as on the webpage.

3. Most important feature thats missing is the data...The headings are in the Excel Doc, but the cells where there is suppose to be data are empty.

Thanks
<!--- My Queries are above here which ain't the problem--->
 
<!--- set vars for special chars --->
<cfset TabChar = Chr(9)>
<cfset NewLine = Chr(13) & Chr(10)>
<!---  IF Button Clicked download EXCEL --->
<CFPARAM NAME="form.ExportExcel" TYPE="any" DEFAULT="0">
<cfif #form.ExportExcel# EQ "Download-Excel">
<!--- use "Content-Disposition" in cfheader for Internet Explorer  --->
<cfheader name="Content-Disposition" value="attachment; filename=FTE_REPORT_#dateformat(Now(),"mm-dd-yyyy")#.xls">
<!--- set content type to invoke Excel --->
<cfcontent type="application/msexcel">
 
</cfif>
 
 
</head>
 
<body>
 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FFCC00"><h1> &nbsp;FTE by Course</h1></td>
</tr>
</table><p>
<!--- Drop Menu --->
<cfinclude template="inc_selector_2.cfm"><br />
<br />
<!--- Export Button --->
<form id="Export2" name="Export2" method="post" ">
  <label>
    <input type="submit" name="ExportExcel" id="ExportExcel" value="Download-Excel" /> </label>
  <br />
</form>
 
 
 
 
<table width="100%" border="1" cellspacing="3" cellpadding="3">
  <tr>
    <td width="52%"> </td>
    <td width="16%" bgcolor="#FFCC00"><h2>2007-08</h2></td>
    <td width="16%" bgcolor="#FFCC00"><h2>2008-09</h2></td>
    <td width="16%"> </td>
  </tr>
  <tr>
    <td><h2>Courses</h2></td>
    <td><h2>FTE</h2></td>
    <td><h2>FTE</h2></td>
    <td><h2>1-Yr Change (#)</h2></td>
  </tr>
  <!--- formatting for decimals #LSNUMBERFORMAT(FTE_89, 0.0000)#--->
  <cfset yr_change = 0>
 
<cfoutput query="get_results78" group="Number">
 
<cfset yr_change = get_results78.FTE_89 - get_results78.FTE_78>
 
  <tr>
    <td class="body">#get_results78.Number# #get_results78.Name#</td>
 
  
    <td class="body">#LSNUMBERFORMAT(get_results78.FTE_78, 0.00)#</td>
    <td class="body">#LSNUMBERFORMAT(get_results78.FTE_89, 0.00)#</td>
    <td><span class="<cfif yr_change LT 0>minus<cfelse>body</cfif>">#LSNUMBERFORMAT(yr_change, 0.00)#</span></td>
  </tr>
 
 </cfoutput>
   <cfset yr_change2 = 0>
 <cfoutput query="get_results89" group="Number">
 <cfset yr_change2 = get_results89.FTE_89 - get_results89.FTE_78>
 <cfif Yr_78 EQ 0>
  <tr>
    <td class="body">
    #get_results89.Number# #get_results89.Name#</td>
    <td class="body">#LSNUMBERFORMAT(get_results89.FTE_78, 0.00)#</td>
    <td class="body">#LSNUMBERFORMAT(get_results89.FTE_89, 0.00)#</td>
    <td> <span class="<cfif yr_change2 LT 0>minus<cfelse>body</cfif>">#LSNUMBERFORMAT(yr_change2, 0.00)#</span>
    </td>
  </tr>
    </cfif>
</cfoutput>
 
  <tr>
    <td class="body"><h2>Totals</h2></td>
    <td class="body">
   
	<!--- Sum for 2007-8 --->
   
	<cfoutput >
    <!---#GrandTotal78#--->
    #LSNUMBERFORMAT(theTotals78, 0.00)#
	</cfoutput>
   
    </td>
    <!--- Sum for 2007-8 --->
    <td class="body">
	<cfoutput  >
    <!---#GrandTotal89#---> 
    #LSNUMBERFORMAT(theTotals89, 0.00)#
	</cfoutput></td>
    <cfset YrTotals = 0>
    <cfset YrTotals = theTotals89 - theTotals78  >
    <td class="body"><cfoutput><span class="<cfif YrTotals LT 0>minus<cfelse>body</cfif>">#LSNUMBERFORMAT(YrTotals, 0.00)#</span></cfoutput></td>
  </tr>
 
</table>
 
       
 
</body>
</html>

Open in new window

Comment
Watch Question

If you're exporting to excel, things like this are superfluous and might be causing the problem:
</head>
 
<body>


<!--- Drop Menu --->
<cfinclude template="inc_selector_2.cfm"><br />
<br />


<!--- Export Button --->
<form id="Export2" name="Export2" method="post" ">
  <label>
    <input type="submit" name="ExportExcel" id="ExportExcel" value="Download-Excel" /> </label>
  <br />
</form>

The only thing you should have, for the Excel version, is that table and all its contents.

System Architect, CF programmer
Commented:
put the table inside the cfsavecontent tag.

Then do this
<cfset hdr_RptID = "Report">
<cfsavecontent variable="Excel">

</cfsavecontent>
do this
<cfoutput>
#Excel#
<cfset xls_filename = "#hdr_RptID#_#now()#.xls">
<CFSET fullfile = "d:\inetpub\wwwroot\c\#xls_filename#">
<cffile action = "write"
         file = "#fullfile#"
         output = "#Excel#"
         nameconflict="overwrite">
      <a target="_blank" href="xls_temp/#xls_filename#"></a><a target="_blank" href="xls_temp/#xls_filename#">Click here to save Excel file</a>

</cfoutput>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
That worked! Thank you!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.