Oh my god. It's full of code!

Turn a query into an excel spreadsheet

Hey everyone. Here is a handy chunk of code I use to turn any query into a download-able excel spreadsheet. I like it pretty well so I figured I’d share.

    <cfsavecontent variable="report">

        <table border="0">
            <tr valign="bottom">
                <cfloop list="#QUERY.columnList#" index="columnName">
                    <th align="center">#columnName#</th>
                </cfloop>
            </tr>    
            
            
            <cfloop from="1" to="#QUERY.recordCount#" index="i">
                <tr valign="top">
                    <cfloop list="#QUERY.columnList#" index="columnName">
                         <td align="left" class="report">
                            <cftry>
                            #QUERY[columnName][i]#
                                <cfcatch type="any">
                                    NULL        
                                </cfcatch>
                            
                            </cftry>                                    
                         </td> 
                    </cfloop>
                </tr>
            </cfloop>
        </table>
    </cfsavecontent>
    
    <cfset Reportname = "Your Data.xls">
    
    <cffile action="write" file="C:\Website\Reports\#Reportname#" output="#report#" nameconflict="overwrite">
    
    <a href="http://YourPage/reports/#reportname#">Download Data</a>

2 responses

  1. Hello, everything is going nicely here and ofcourse every one
    is sharing facts, that’s truly good, keep up writing.

    April 17, 2013 at 2:07 pm

  2. Just wish to say your article is as surprising. The clearness in your publish is just great
    and i could think you’re a professional in this subject. Fine together with your permission allow me to grasp your RSS feed to keep updated with drawing close post. Thank you a million and please continue the enjoyable work.

    June 7, 2013 at 5:49 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s