How can ColdFusion generate an excel file?

Because recent Excel products support HTML table format, getting ColdFusion to generate and Excel file can be as simple as creating your HTML tables and then using the <cfcontent> tag to set the mime type of your newly generated Excel file.

The following code sample taken from the CF 7 documentation shows a sample of using <cfheader> and <cfcontent> to push a dynamic Excel file to the browser (prompt the user whether to save Excel the file or open it in a browser).

<cfheader name="Content-Disposition" value="inline; filename=acmesalesQ1.xls">
<cfcontent type="application/vnd.msexcel">
<table border="2">
<tr><td>Month</td><td>Quantity</td><td>$ Sales</td></tr>
<tr><td>January</td><td>80</td><td >$245</td></tr>
<tr><td>February</td><td>100</td><td>$699</td></tr>
<tr><td>March</td><td>230</td><td >$2036</td></tr>
<tr><td>Total</td><td>=Sum(B2..B4)</td><td>=Sum(C2..C4)</td></tr>
</table>

Another option to consider is the Jakarta POI project: http://jakarta.apache.org/poi/. As per the project home page: The POI project consists of APIs for manipulating various file formats based upon Microsoft's OLE 2 Compound Document format using pure Java. In short, you can read and write MS Excel files using Java.

This question was written by Jeremy Petersen
It was last updated on July 28, 2006.

Categories

Miscellaneous

Comments

comments powered by Disqus