Join 300,490 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,816 people online right now. Registration is fast and FREE... Join Now!
hi i am working on a button that when clicked will export data to an excel spreadsheet. i have been using a coworkers code as an example but it doesnt seem to be working so i am looking for more input. i have posted the code. also i have tested the url.reportkey variable and it is correctly storing the report key. i am fairly new to coldfusion and javascript so let me know if i am way off track or not. i know the problem is in the second function createExcel() some where. thanks in advance!
i am fairly new to coldfusion and javascript so let me know if i am way off track or not. i know the problem is in the second function createExcel() some where. thanks in advance!
Yes, you are a bit off track. Javascript is client side and cannot run CF code. ColdFusion runs on the server and Javascript runs in the browser. Neither one knows anything about the other.
To execute any server side code from javascript, you need to submit another request to the server. You could create another page, say "createExcel.cfm" and then request that page from javascript.
ok well its starting to make some sense now. i guess i shouldn't have trusted my coworkers code. lol. my new question is could i just take the coldfusion code in the function createExcel() and have it run in a page load event on a new page???
QUOTE(sansclue @ 31 Mar, 2009 - 06:24 AM)
QUOTE(iamjesus1342027 @ 31 Mar, 2009 - 06:04 AM)
i am fairly new to coldfusion and javascript so let me know if i am way off track or not. i know the problem is in the second function createExcel() some where. thanks in advance!
Yes, you are a bit off track. Javascript is client side and cannot run CF code. ColdFusion runs on the server and Javascript runs in the browser. Neither one knows anything about the other.
To execute any server side code from javascript, you need to submit another request to the server. You could create another page, say "createExcel.cfm" and then request that page from javascript.
i also have this page as part of my exporting project. this page has a button that gets the report key from the database and stores it in a variable. the button event then opens a new window (that has the previous code on it) and passes the report key to it using url.report key.
i guess i shouldn't have trusted my coworkers code. lol. my new question is could i just take the coldfusion code in the function createExcel() and have it run in a page load event on a new page???
Sure, assuming you generate the right date and headers. I assume that is part of what this custom tag is doing: <CF_PROMPT4DOWNLOAD FILENAME="#tempFileName#" DIRECTORY="#output#">.
As far as your coworker's code, I couldn't say. Maybe they were using <cfscript> instead of <script> tags, or possibly using this code on page load, .. which would make more sense.
thanks a lot for your help so far. im getting it sorted out slowly. one question though. an easy one too. how do you set up the page load even in coldfusion?
QUOTE(sansclue @ 31 Mar, 2009 - 09:45 AM)
QUOTE(sansclue @ 31 Mar, 2009 - 06:24 AM)
i guess i shouldn't have trusted my coworkers code. lol. my new question is could i just take the coldfusion code in the function createExcel() and have it run in a page load event on a new page???
Sure, assuming you generate the right date and headers. I assume that is part of what this custom tag is doing: <CF_PROMPT4DOWNLOAD FILENAME="#tempFileName#" DIRECTORY="#output#">.
As far as your coworker's code, I couldn't say. Maybe they were using <cfscript> instead of <script> tags, or possibly using this code on page load, .. which would make more sense.
thanks a lot for your help so far. im getting it sorted out slowly. one question though. an easy one too. how do you set up the page load even in coldfusion?
Assuming this is a cfm separate page, whose only job is to generate reports for download, you don't really need to do anything special. Just run your db queries, then generate the headers and output the data. The mime headers will take care of the rest. They basically tell the browser "Here some Excel-like data". If the browser is configured to recognize Excel files (most are), it will either open the file or prompt to save it to disk.
alright i have been out on the web reading all kinds of things and have some new code. i got the code from http://www.cftopper.com/index.cfm/blogId/1/m/4/y/2007 and would like to give him credit. i have made some changes to the code to try to get my specific data but no major changes. unfortunately the code is not working. when i load the page nothing happens at all. any and all suggestions are welcome. Thanks!!!
<!-- Gets the data --> <cfquery name="theQuery" datasource="#session.sysdb#"> select * from DictionaryReport where ReportKey = '#url.reportkey#' order by ReportKey </cfquery>
<!-- Set up the file and location for spreadsheet to be saved in --> <cfset guid = CreateUUID()> <cfset location = "c:\temp\"> <cfset filename = location & "ReportData_" & guid & ".xls"> <cfset REQUEST.headerDoneFlag = true> <cfheader name="Content-type" value="application/octet-stream"> <cfheader name="Content-Disposition" value="attachment;filename=""#ARGUMENTS.theFilename#"""> <cfsavecontent variable="output"> <cfset generateExcel(ARGUMENTS.theQuery, ARGUMENTS.columnList, "save")> </cfsavecontent><cfoutput>#output#</cfoutput><cfabort> <cffile action="WRITE" file="#filename#" output="#output#"> <cfcontent type="application/msexcel" file="#filename#" deleteFile="Yes">
well i need the data to look nice once it is downloaded so it can be printed out and used. but a tab delimited file would work great cause i dont really need it to be in excel. the data isnt gonna be much more than a list so there really isnt any need for it to be an excel file. i wont be running any kind of analysis on it or anything.
QUOTE(sansclue @ 1 Apr, 2009 - 11:52 AM)
Can I ask, are you just trying to create a CSV or tab delimited file for download?
well i need the data to look nice once it is downloaded so it can be printed out and used. but a tab delimited file would work great cause i dont really need it to be in excel. the data isnt gonna be much more than a list so there really isnt any need for it to be an excel file. i wont be running any kind of analysis on it or anything.
Well .. personally I would try generating a basic tab delimited file on my own first. So I first understood how to generate excel like files on the fly. Once I understood that, then I would try and make it look nice .. or use a pre-made component by someone else. It is much harder to work with someone else's code (as good as it might be) when you don't understand the underlying code.
alright. i have figured out to make a csv file and have some new code for an excel file. this code almost works. when the page loads a window pops up asking if you want to save or open the excel file. the problem occurs when you click either of these options. when you do an error message pops up saying you cannot do that because the file does not exist, the file is being used by another program, or a file with the same name already exists. i am not sure what to do about this error. i have noticed that the name of the file that is trying to be opened is the same as the url for the page. i dont think this is right and i specify a file name in my code but apparently it is being recognized. any ideas or suggestions would be greatly appreciated. thanks!!!
CODE
<!-- use cfsetting to block output of HTML outside of cfoutput tags --> <cfsetting enablecfoutputonly="Yes"> <cfparam name="url.reportkey" default="None"> <cfsetting showdebugoutput="no">
<!-- get employee info --> <cfquery name="getReport" datasource="#session.sysdb#"> SELECT * FROM DictionaryReport </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/vnd.ms-excel">
<!-- suggest default name for XLS file --> <!-- use "Content-Disposition" in cfheader for Internet Explorer --> <cfheader name="Content-Disposition" value="filename=Report_Data.xls">
<!-- output data using cfloop & cfoutput --> <cfloop query="getReport"> <cfoutput>#ReportKey##TabChar##ReportName# #TabChar##ReportDescription##TabChar##Module##NewLine#</cfoutput> </cfloop>
when i use inline i receive the same error message as before. when i use attachment it does not work at all. the window never pops up for me to open or save the file.
well now that i have figured out a way to create the excel spreadsheet i am looking to format it using code. does anyone know how to do this?? i would like to format text, such as making the column headers bold and a bigger font. also i would like to edit the width of the cells to set them to a specific size. let me know any ideas. thanks!!
just in case anyone is interested i have figured out how to create an excel file with multiple tabs. i got the code from this website, http://cfsilence.com/blog/client/index.cfm...with-Coldfusion, so i would like to give them credit. but here it is. my code also includes a quasi-loop to enter all of the data from a query.
CODE
<cfsetting showdebugoutput="false"> <cfheader name="content-disposition" value="inline;filename=#url.reportkey#_Report_Data.xls"> <cfcontent type="application/msexcel"> <cfparam name="url.reportkey" default="None"> <cfquery name="getReport" datasource="#session.sysdb#"> SELECT * FROM DictionaryReport WHERE ReportKey = '#url.reportkey#' </cfquery> <cfquery name="getTable" datasource="#session.sysdb#"> SELECT * FROM DictionaryTable WHERE ReportKey = '#url.reportkey#' </cfquery> <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">