How To generate DOC Using Coldfusion

Coast LineCEO
CERTIFIED EXPERT
Published:
The technique is by far very Simple!

How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet with you guys how did I do that.   Using a simple technique to make work from query to DOC using Coldfusion:

Here is the coding sample…

In the code below, we are using the component where we are fetching the query results and putting them in the endUsers variable.  Now we run a simple query and below running will generate the code as we expected.

The below code is provided with the HELP of SidFishes (an EE Coldfusion Expert)
<cfset tools = CreateObject("component",".tools")>
                      <cfset endUsers = #tools.UsersArea()#>
                      <cfset ExcelFileName = "#DateFormat(Now(), 'mmddyyyy')#.doc">
                      <cfcontent type="application/msword">
                      <cfheader name="Content-Disposition" value="filename=menu_#ExcelFileName#">
                      <cfoutput query="endUsers">
                      <table cellpadding="0" cellspacing="0" border="1">
                         <tr>
                            <th>Created By</th>
                            <th>Created Date</th>
                            <th>Status</th>
                         </tr> 
                         <tr>
                            <td>#endUsers.firstname#</td>
                            <td>#endUsers.lastname#</td>
                            <td>#endUsers.nemail#</td>
                          </tr>
                         </table>
                      </cfoutput>

Open in new window


This may not be the best technique but it works.  Another technique of doing the same is which I tried my way is listed below:

<cfset tools = CreateObject("component",".tools")>
                      <cfset endUsers = #tools.UsersArea()#>
                      <cfsavecontent variable="docfile">
                      <table border="1">
                        <tr>
                          <th>First Name</th>
                          <th>Last Name</th>
                          <th>Email Address</th>
                        </tr>
                        <cfoutput query="endUsers">
                          <tr>
                            <td>#endUsers.firstname#</td>
                            <td>#endUsers.lastname#</td>
                            <td>#endUsers.nemail#</td>
                          </tr>
                        </cfoutput>
                      </table>
                      </cfsavecontent>
                      <cfheader name="content-disposition" value="inline;filename=ToDoc.doc">
                      <cfcontent type="application/msword" >
                      <cfoutput>#docfile#</cfoutput>

Open in new window


That’s Pretty Simple Techniques, try it and You will be amazed to see its results! The technique we presented is far by very basic technique. Although there are many custom tags available which can by far generate good results through the use of CSS and many HTML Tag.  But they are paid ones!

This is the simple technique in which we can generate our result in the doc File!  For quick view.  If you are not really serious about the formatting and want to show it as simple and as fast you can. Try the technique which suits you and enjoy.
2
3,276 Views
Coast LineCEO
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.