Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2894
  • Last Modified:

Need help exporting Coldfusion to Excel?

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

0
tribalboy3000
Asked:
tribalboy3000
1 Solution
 
duncancummingCommented:
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.

0
 
erikTsomikSystem 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>
0
 
tribalboy3000Author Commented:
That worked! Thank you!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now