Hi, I am new to ColdFusion. Can anyone suggest me the correct way to implement export functionality in ColdFusion application. Below is the code - <cfsetting enablecfoutputonly="Yes"> <cfquery name="queryData" datasource="#request.dsn#"> SELECT * from EMPLOYEE </cfquery> <cfcontent type="application/msexcel"> <cfheader name="Content-Disposition" value="filename=test.xls"> <cfoutput> <table border="2"> <tr> <td> ID </td><td> Name </td> </tr> <cfloop query="queryData"> <tr> <td>#id#</td><td>#name#</td> </tr> </cfloop> </table> </cfoutput> Problem is - 1) it doesn't appear as normal excel file. It looks like plain file.(doesn't appear lines of rows and column) 2) HTML contents of my index.cfm file gets uploaded in this excel file. How to avoid this HTML contents? I want to export query result in column-row format in excel sheet. Thanks in advance. Regards, Manjusha
I think the problem is your content type...Instead of "application/msexcel" for excel...try this..."application/vnd.ms-excel" That's what I use and it works perfectly
Still it's not working. I am getting same output. Requirement - Export query result to excel sheet. But now I am just trying to open excel file on click of button. 1)If I try to run below cfm file from wwwroot/Test folder (outside the project folder). I get popup to open the excel file and I am able to open it. <cfsetting enablecfoutputonly="Yes"> <cfsetting showdebugoutput="false" /> <cfcontent type="application/msexcel"> <cfheader name="Content-Disposition" value="filename=test.xls"> 2)But if I try to run same code in my application, I am not able to open excel file in proper format .HTML contents of my index.cfm file (which is in wwwroot/project folder) gets uploaded in excel file. Please revert back if you get any solution. Can you tell me the all steps the way you have implemented this feature? Thanks in advance.
I've got a couple of applications that use this. One is a report builder, and the other emails an excel itinerary as an attachment. Both work a little different. The report builder pops up a download box just like you were downloading a file from the internet and asks if you want to save or open the excel file. The email program actually copies a blank excel document, puts the data inside, renames it, attaches it to an email and sends it out. Either way, the code is pretty similar. Try this and see what you get. This is from my report builder, but I added your query and table structure. <cfquery name="queryData" datasource="#request.dsn#"> SELECT * FROM EMPLOYEE </cfquery> <body> <cfheader name="Content-Disposition" value="inline; filename=Employee_Report.xls"> <cfcontent type="application/vnd.ms-excel"> <table border="2"> <tr> <td> ID </td><td> Name </td> </tr> <cfoutput query="queryData"> <tr> <td>#id#</td><td>#name#</td> </tr> </cfoutput> </table> </cfcontent> </body> Code (markup):
This guy has a custom function for it too. Maybe it will help you: http://www.cftopper.com/index.cfm?blogpostid=180
Try placing your Cfheader / cf content code at the bottom of the page so that it is the last bit of code to execute. Just before the </body> tag. By having it at the top of the page it has nothing to convert.
Another CF noob with a question. I used the code from Chadzilla and it works, but even though I name the document export.xls, it still gives it a .cfm. Excel can open and display, but I'm sure our users won't like the extra errors popping up. Any ideas? Thanks in advance.
manjusha.pate The code works, when you view it on-line it views as html, if you save it to disk, and open it with excel you will get the look and feel your looking for. A quick question, what is the name of the template your code is in? is it index.cfm or are using a frame work such as fusebox / coldbox/ mach1 and its loading that index file?