I'm using the code below to export data from a database to an Excel sheet. What I want to do is have a certain set of fields be exported to a different sheet in the same workbook. The end result being that each person's data is on a separate sheet rather than all combined on the same sheet like this code is doing. Any thoughts as to how I can write a formula or a piece of ColdFusion than will let me do this?
Also I'm using ColdFusion 8 so I can't use the cfspreadsheet function.
<!--- use cfsetting to block output of HTML
outside of cfoutput tags --->
<cfsetting enablecfoutputonly="Yes">
<!--- set content type to invoke Excel --->
<cfcontent type="application/msexcel">
<!--- suggest default name for XLS file --->
<!--- use "Content-Disposition" in cfheader for
Internet Explorer --->
<cfheader name="Content-Disposition" value="filename=export.xls">
<!--- output data using cfloop & cfoutput --->
<cfquery name="qquestionnaire_export" datasource="mydatabase">
Select * from registration
</cfquery>
<cfoutput>
<table>
<tr><td align="center">Transfer Registration Questionnaire</td>
<td></td>
</tr>
<th>Credit Category</th>
<th>Completed Degree</th>
<th>Highest Degree</th>
</cfoutput>
<cfloop query="qquestionnaire_export">
<cfoutput>
<tr>
<td align="center">#credit_category#</td>
<td align="center">#completed_degree#</td>
<td align="center">#highest_degree#</td>
</tr>
</cfoutput>
</cfloop>
<cfoutput><tr><td height="10"></td></tr></cfoutput>
<cfoutput></table></cfoutput>
I know this is an old topic, but this solution would have been helpful if I had found it earlier. However, here is a basic function call that should allow any number of queries put into an excel sheet with multiple tabs.
Apache POI is def the way to go. http://poi.apache.org/
Take the time to read the documentation and do some tests bc it gives you total control rather than trying to fudge HTML tables with CSS.
If you can avoid using this method, it would be best. Excel is smart enough to output your HTML in an 'spreadsheet' looking output but it is still an HTML document with an xls extension. I've had several problems using spreadsheets created like this that expect an actual Excel file. In ColdFusion, if you try reading this file with cfspreadsheet you will most likely get a an error that says something like
expected binary input file
(or something close to that) and you'll have to use excel tosave as
to make it an actual Excel spreadsheet. Not to mention this is a static output. You can't include any formulas or any fun stuff like iKnowKungFoo indicated.Since that doesn't answer your question, in addition to the POI tools you could also look into other BI tools, like JasperSoft, Crystal Reports, or if you're using Oracle or MS SQL Server you could look into Oracle's BI tools or SSRS for SQL Server. There are several BI tools that are free without a support option, I believe JasperSoft is one of them.
Back to ColdFusion, I've never tried using CFReport with report builder to create an Excel formated report but in CF 8 the excel format is available. I'm not sure if you can create multiple sheets with it, I would assume not considering the below snipit but it may be worth looking into. I couldn't find anything but it's early, I'm sick, and my google-foo is a bit slow this morning.
If you can't use
<cfspreadsheet>
, then I suggest using the Apache POI project instead of the simple "HTML as Excel via CFCONTENT" approach. POI enables you to create actual Excel spreadsheets with all of the fun that's associated with them.Ben Nadel has a CFC wrapper that exposes the multi-sheet parts of the API.
http://www.bennadel.com/projects/poi-utility.htm