Dynamic Excel file from ColdFusion

by @jehiah on 2004-09-03 11:22UTC
Filed under: All , ColdFusion

It comes up every now and then about generating a Microsoft Excel File from a ColdFusion Page. Read on for a simple solution to this task that doesn’t involve com objects, csv files, or saving files to the server.

As it turns out, you can open a simple HTML page in excel and it will interpret basic tables as an excel sheet. We can build on this knowledge and force Excel to automagically open up the html page from the web. (It’s a whole different ball game if you do not have excel installed).

First we set the content type so IE or Firefox pass the request on to Excel

<cfcontent type="application/vnd.ms-excel">

Next we put together a simple HTML page

<h2>Page Title</h2>
<table border="1" cellpadding="1" cellspacing="2">
<tr>
    <th>Column 1</th>
    <th>Column 2</th>
</tr>
<tr>
    <td>Data1</td>
    <td>Data 2</td>
</tr> 
</table>

That’s all there is to it. The browser will open it up in excel and the user can save it as an .xls file if they care to.

Subscribe via RSS ı Email
© 2023 - Jehiah Czebotar