<script src="_scripts/dictionary.js" type="text/javascript" language="javascript"></script>
<script src="../../../_scripts/dictionary.js" type="text/javascript" language="javascript"></script>
<cfoutput>
<cfparam name="url.reportkey" default="None">
<div id="selectreport">
<table align="left">
<tr>
<td>
<button name="exportreport" style="font-weight:bold;" onclick="Excel()">Export Report to Excel</button>
</td>
</tr>
</table>
</div>
<script>
function Excel()
{
var reportkey = '#url.reportkey#';
if(reportkey == null)
{
alert('not working');
return;
}
alert(reportkey);
<cftry>
createExcel();
<cfcatch type="any">
<cfset local.name = 'dictionary.cfm'>
<cfset sCurrent = #CFCATCH.TAGCONTEXT[1]#>
<cflog text=" Error in #local.name# : #cfcatch.message# : Caught an exception, type = #CFCATCH.TYPE# : #CFCATCH.detail# : The error occurred near statement/line - #sCurrent['ID']# (#sCurrent['LINE']#,#sCurrent['COLUMN']#) #sCurrent['TEMPLATE']# " type="Error" file="customReports" date=now time=now>
</cfcatch>
</cftry>
}
function createExcel()
{
<cfquery name="getReportData" datasource="#session.sysdb#">
select *
from DictionaryReport
where ReportKey = #reportkey#
order by ReportKey
</cfquery>
<cfobject name="xls" component="_components/excel">
<cfset sheets = ArrayNew(1)>
<cfif isDefined("session.OutputDirectory")>
<cfset output = session.OutputDirectory>
<cfelse>
<cfset output = "c:\temp\">
</cfif>
<cfset columnListArray = getMetaData(getReportData())>
<cfset columnList = "">
<cfset columnNames = "">
<cfset columnNames = ListAppend(columnNames, 'Report Key')>
<cfset columnNames = ListAppend(columnNames, 'Report Name')>
<cfset columnNames = ListAppend(columnNames, 'Report Description')>
<cfset columnNames = ListAppend(columnNames, 'Module')>
<cfset columnNames = ListAppend(columnNames, 'Report Type')>
<cfset columnNames = ListAppend(columnNames, 'Entity')>
<cfset columnNames = ListAppend(columnNames, 'Version Added')>
<cfset columnNames = ListAppend(columnNames, 'Visible')>
<cfset columnNames = ListAppend(columnNames, 'Main Table')>
<cfset columnNames = ListAppend(columnNames, 'Display Order')>
<cfloop index="i" from="1" to="#arrayLen(columnListArray)#" >
<cfset columnList = ListAppend(columnList, columnListArray[i].name)>
</cfloop>
<cfset sheets[1] = StructNew()>
<cfset sheets[1].Query = getReportData()>
<cfset sheets[1].ColumnList = columnList>
<cfset sheets[1].ColumnNames = columnNames>
<cfset sheets[1].SheetName = "Report Data">
<cfset tempFileName = "ReportData_#RandRange(100,10000)#.xls">
<cfset test = xls.WriteExcel('#output##tempFileName#','#sheets#',',')>
<cfheader name="Pragma" value="">
<cfheader name="Cache-control" value="">
<CF_PROMPT4DOWNLOAD FILENAME="#tempFileName#" DIRECTORY="#output#">
}
</script>
</cfoutput>
24 Replies - 10485 Views - Last Post: 09 April 2009 - 10:17 AM
#1
exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 07:04 AM
Replies To: exporting from sql database to excel spreadsheet
#2
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 07:24 AM
iamjesus1342027, on 31 Mar, 2009 - 06:04 AM, said:
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.
#3
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 08:46 AM
sansclue, on 31 Mar, 2009 - 06:24 AM, said:
iamjesus1342027, on 31 Mar, 2009 - 06:04 AM, said:
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.
#4
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 10:28 AM
<script src="_scripts/dictionary.js" type="text/javascript" language="javascript"></script>
<script src="../../../_scripts/dictionary.js" type="text/javascript" language="javascript"></script>
<cfoutput>
<script>
function getExcel(){
var reportkey = document.getElementById('reporttypeselect').value;
if(reportkey == '')
{
alert('you must select a report');
return;
}
alert(reportkey);
window.open('excelAction.cfm?reportkey=' + reportkey);
}
</script>
<!---Determine Edit Permissions. --->
<cfif isDefined("session.report.permission.Admin.Management.User.Dictionary")>
<cfset onchangeaction = "selectReport(this);">
<cfelse>
<cfset onchangeaction = "">
</cfif>
<cfquery name="qryReports" datasource="#session.sysdb#">
SELECT ReportKey, ReportName, ReportDescription, [Module], ReportType, Entity
FROM DictionaryReport
<cfif not isDefined("session.report.permission.Admin.Management.User.Dictionary")>
WHERE Visible = <cfqueryparam value="1" cfsqltype="bit">
</cfif>
ORDER BY DisplayOrder, ReportName
</cfquery>
<div id="selectreport">
<table align="center">
<tr>
<td colspan="2" align="center" style="font-weight:bold;font-size:16px;">Data Dictionary</td>
</tr>
</table>
<table align="left">
<tr>
<td style="font-weight:bold;font-size:14px;" nowrap><label class='reportlabel'>Select a Report Type to Export to Excel:</label></td>
<td><select name="reporttypeselect" id="reporttypeselect" onchange="#onchangeaction#" style="width:300px;">
<option value="">Choose A Report</option>
<cfloop query="qryReports">
<option value="#qryReports.ReportKey#">#qryReports.Module# #qryReports.ReportName#</option>
</cfloop>
</select>
<button name="exportreport" style="font-weight:bold;" onclick="getExcel()">Export Report to Excel</button>
</td>
</tr>
</table>
</div>
</cfoutput>
#5
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 10:45 AM
sansclue, on 31 Mar, 2009 - 06:24 AM, said:
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.
#6
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 10:54 AM
sansclue, on 31 Mar, 2009 - 09:45 AM, said:
sansclue, on 31 Mar, 2009 - 06:24 AM, said:
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.
#7
Re: exporting from sql database to excel spreadsheet
Posted 31 March 2009 - 11:14 AM
Quote
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.
Excel example, at the bottom of this page
http://livedocs.adob...cs/00000232.htm
#8
Re: exporting from sql database to excel spreadsheet
Posted 01 April 2009 - 12:23 PM
<cffunction name="generateExcel" output="Yes"> <cfargument name="theQuery" type="query"> <cfargument name="columnList" type="string" default=""> <cfargument name="mode" type="string" default="save"> <cfargument name="theFilename" type="string" default="report.xls"> <cfset var TAB = chr(9)> <cfset var i = 1> <cfset var headerArray = ArrayNew(1)> <cfparam name="url.reportkey" default="None"> <cfsetting enablecfoutputonly="Yes"> <!-- Dump all content before this fn call --> <cfcontent reset="yes"> <!-- Set up the columnList --> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Report Key')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Report Name')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Report Description')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Module')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Report Type')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Entity')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Version Added')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Visible')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Main Table')> <cfset ARGUMENTS.columnList = ListAppend(ARGUMENTS.columnList, 'Display Order')> <!-- 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"> <!-- Write the header row --> <cfloop index="i" from="1" to="#ListLen(ARGUEMENTS.columnList)#"> <cfset item = Trim(ListGetAt(ARGUEMENTS.columnList, i))> <cfset headerArray[ArrayLen(headerArray )+1] = item> <cfoutput>#item##TAB#</cfoutput> </cfloop> <cfoutput>#chr(13)#</cfoutput> <!-- Write all the sub rows ---> <cfloop query="ARGUMENTS.theQuery"> <cfloop index="i" from="1" to="#ArrayLen(headerArray)#"> <cfset dbVal = Trim(ARGUMENTS.theQuery[headerArray[i]][CurrentRow])> <cfif IsDate(dbVal)> <cfset dbVal = DateFormat(dbVal, "mm/dd/yyyy")> <cfelse> <cfset dbVal = replaceList(dbVal,"#TAB#,#chr(10)#,#chr(13)#"," , , ")> </cfif> <cfoutput>#dbVal##TAB#</cfoutput> </cfloop> <cfoutput>#chr(13)#</cfoutput> </cfloop> <cfsetting enablecfoutputonly="No"> </cffunction>
#9
Re: exporting from sql database to excel spreadsheet
Posted 01 April 2009 - 12:52 PM
#10
Re: exporting from sql database to excel spreadsheet
Posted 01 April 2009 - 12:59 PM
sansclue, on 1 Apr, 2009 - 11:52 AM, said:
#11
Re: exporting from sql database to excel spreadsheet
Posted 01 April 2009 - 01:18 PM
iamjesus1342027, on 1 Apr, 2009 - 11:59 AM, said:
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.
#12
Re: exporting from sql database to excel spreadsheet
Posted 01 April 2009 - 01:20 PM
#13
Re: exporting from sql database to excel spreadsheet
Posted 06 April 2009 - 06:23 AM
<!-- 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>
#14
Re: exporting from sql database to excel spreadsheet
Posted 06 April 2009 - 09:19 AM
iamjesus1342027, on 6 Apr, 2009 - 05:23 AM, said:
<cfoutput> <cfheader name="Content-Disposition" value="attachment; filename=Report_Data.xls"> <cfcontent type="application/vnd.ms-excel"> <cfloop query="getReport"> #ReportKey##TabChar##ReportName##TabChar##ReportDescription##TabChar##Module##NewLine# </cfloop> </cfoutput>
I do not receive any errors with the code. But you should add either "attachment" or "inline" to the Content-Disposition.
#15
Re: exporting from sql database to excel spreadsheet
Posted 06 April 2009 - 09:22 AM
|
|

New Topic/Question
Reply




MultiQuote



|