Twitter: raymondcamden


Address: Lafayette, LA, USA

ColdFusion Sample - Create an Excel File

06-01-2011 38,316 views ColdFusion

In this ColdFusion sample, I'll demonstrate a simple way to take form input and generate an Excel spreadsheet file. This is a simple example with a very basic form, but hopefully it will give you enough to go by to create your own similar application.

Let's begin by creating a simple form. This form is going to ask for 10 rows of data that mimics the Excel sheet I uploaded in my previous blog entry. It will ask for a name and the number of beers, vegetables, fruits, and meats consumed.

view plain print about
1<form action="test2.cfm" method="post">
2    <table>
3        <tr>
4            <th>Name</th>
5            <th>Beers</th>
6            <th>Vegetables</th>
7            <th>Fruits</th>
8            <th>Meats</th>
9        </tr>
10    <cfloop index="x" from="1" to="10">
11        <cfoutput>
12        <tr>
13            <td><input type="text" name="name_#x#"></td>
14            <td><input type="text" name="beers_#x#"></td>
15            <td><input type="text" name="veggies_#x#"></td>    
16            <td><input type="text" name="fruits_#x#"></td>    
17            <td><input type="text" name="meats_#x#"></td>
18        </tr>
19        </cfoutput>
20    </cfloop>
21    </table>
22    <input type="submit" name="doit" value="Create Excel File">
23</form>

This form could be more dynamic of course. You could present 3 rows and use jQuery (or another less capable JavaScript framework) to easily add additional rows for input. But to keep things simple the form will just create 10 rows. Here's how it looks - and again - more work could be done to make this friendlier to the end user.

Ok - now to generate the spreadsheet. Let's look at an example.

view plain print about
1<cfset q = queryNew("Name,Beers,Vegetables,Fruits,Meats", "cf_sql_varchar,cf_sql_integer,cf_sql_integer,cf_sql_integer,cf_sql_integer")>
2<cfloop index="x" from="1" to="10">
3    <cfset queryAddRow(q)>
4    <cfset querySetCell(q, "Name", form["name_#x#"])>
5    <cfset querySetCell(q, "Beers", form["beers_#x#"])>
6    <cfset querySetCell(q, "Vegetables", form["veggies_#x#"])>
7    <cfset querySetCell(q, "Fruits", form["fruits_#x#"])>
8    <cfset querySetCell(q, "Meats", form["meats_#x#"])>
9</cfloop>
10
11<cfset filename = expandPath("./myexcel.xls")>
12<cfspreadsheet action="write" query="q" filename="#filename#" overwrite="true">

We begin by turning the form data into a query. This is done by looping from 1 to 10 and grabbing the relevant values from the Form struct. You could do a trim and other checks here as well. The real magic of this code block is the last line. All it takes to create a spreadsheet in ColdFusion is one line. You pass in the query, filename, and... that's it! While this works, the output is a bit plain. Let's now look at a slight modification.

view plain print about
1<!--- Make a spreadsheet object --->
2<cfset s = spreadsheetNew()>
3<!--- Add header row --->
4<cfset spreadsheetAddRow(s, "Name,Beers,Vegetables,Fruits,Meats")>
5<!--- format header --->    
6<cfset spreadsheetFormatRow(s,
7        {
8            bold=true,
9            fgcolor="lemon_chiffon",
10            fontsize=14
11        },
12        1)
>

13
14<!--- Add query --->
15<cfset spreadsheetAddRows(s, q)>
16<cfset spreadsheetWrite(s, filename, true)>

This version is a bit more complex. We begin creating a blank spreadsheet object. Next we add the header row. We can format both cells and rows, and in this example I've gone ahead and formatted the header. You have many options but I used just bold, fgcolor, and fontsize. (No one make any comments about lemon chiffon, it rocks.)

Next I add the data using spreadsheetAddRows and write out the data. Here's a quick example of the result.

Here's the entire code template.

view plain print about
1<cfif not structKeyExists(form, "doit")>
2    
3    <form action="test2.cfm" method="post">
4        <table>
5            <tr>
6                <th>Name</th>
7                <th>Beers</th>
8                <th>Vegetables</th>
9                <th>Fruits</th>
10                <th>Meats</th>
11            </tr>
12        <cfloop index="x" from="1" to="10">
13            <cfoutput>
14            <tr>
15                <td><input type="text" name="name_#x#"></td>
16                <td><input type="text" name="beers_#x#"></td>
17                <td><input type="text" name="veggies_#x#"></td>    
18                <td><input type="text" name="fruits_#x#"></td>    
19                <td><input type="text" name="meats_#x#"></td>
20            </tr>
21            </cfoutput>
22        </cfloop>
23        </table>
24        <input type="submit" name="doit" value="Create Excel File">
25    </form>
26        
27<cfelse>
28    
29    <cfset q = queryNew("Name,Beers,Vegetables,Fruits,Meats", "cf_sql_varchar,cf_sql_integer,cf_sql_integer,cf_sql_integer,cf_sql_integer")>
30    <cfloop index="x" from="1" to="10">
31        <cfset queryAddRow(q)>
32        <cfset querySetCell(q, "Name", form["name_#x#"])>
33        <cfset querySetCell(q, "Beers", form["beers_#x#"])>
34        <cfset querySetCell(q, "Vegetables", form["veggies_#x#"])>
35        <cfset querySetCell(q, "Fruits", form["fruits_#x#"])>
36        <cfset querySetCell(q, "Meats", form["meats_#x#"])>
37    </cfloop>
38    
39    <cfset filename = expandPath("./myexcel.xls")>
40    <!---
41    <cfspreadsheet action="write" query="q" filename="#filename#" overwrite="true">
42    --->

43    <!--- Make a spreadsheet object --->
44    <cfset s = spreadsheetNew()>
45    <!--- Add header row --->
46    <cfset spreadsheetAddRow(s, "Name,Beers,Vegetables,Fruits,Meats")>
47    <!--- format header --->    
48    <cfset spreadsheetFormatRow(s,
49            {
50                bold=true,
51                fgcolor="lemon_chiffon",
52                fontsize=14
53            },
54            1)
>

55    
56    <!--- Add query --->
57    <cfset spreadsheetAddRows(s, q)>
58    <cfset spreadsheetWrite(s, filename, true)>
59        
60    Your spreadsheet is ready. You may download it <a href="myexcel.xls">here</a>.
61
62</cfif>

Your Homework!

In the code template above, I save the file and use HTML to link to it. Modify this template to instead serve the content to the user immediately. Also - use another ColdFusion function to specify a numeric formatting for the columns (all of the ones but name).

Related Blog Entries