Import and Export Data From Excel Using ColdFusion
page 4 of 5
by Debjani Mallick
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 6456/ 190

Writing to an excel sheet (method 2)
<cffunction name="writeExcelSheet" access="public" 
  output="false" returntype="void">
      <cfargument name="tableName" required="true" type="string" />
      <cfargument name="dsnName" required="true" type="string" />
      <cfargument name="fileName" required="true" type="string" />
      <cfscript>
            var getRecords = '';
            var listOfColumns = '';
            var tabChar = '';
            var newLine = '';
            var index = '';
            var value = '';
      </cfscript>
      <cfsetting enablecfoutputonly="Yes">
      <cfquery name="getRecords" datasource="#arguments.dsnName#">
            select * from #arguments.tableName#
      </cfquery>
 
      <cfif getRecords.RecordCount>
            <cfsetting showdebugoutput="false">
            <cfset listOfColumns = getRecords.columnList>
            <cfset TabChar = Chr(9)>
            <cfset NewLine = Chr(13) & Chr(10)>
            <cfsavecontent variable="value">
                  <cfloop list="#listOfColumns#" index="index">
                        <cfoutput>#index##TabChar#</cfoutput>
                  </cfloop>
            </cfsavecontent>
            <cffile action="write" file="#arguments.fileName#" output="#value#" >
 
            <!--- output query data using cfloop & cfoutput --->
            <cfset value = ''>
            <cfsavecontent variable="value">
                  <cfloop query="getRecords">
                        <cfloop list="#listOfColumns#" index="index">
                              <cfoutput>#getRecords['#index#'][CurrentRow]##TabChar#</cfoutput>
                        </cfloop>
                        <cfoutput>#NewLine#</cfoutput>
                  </cfloop>
            </cfsavecontent>
            <cffile action="append" file="#arguments.fileName#" output="#value#">
      </cfif>
</cffunction>

Explanation

In the above function, we need three arguments – tableName which is the name of the table from which we need to extract the data and save to the excelsheet, dsnName is the datasource name for connecting to the database where the table is present, fileName is the full path along with the excelsheet name where the data is to be written. This uses all the ColdFusion functions to write to an excelsheet. Here we are simply querying the database and if we have records, we are setting the first row of the sheet with the column names of the table and then we are looping over the records, and creating one row for each record. We have used cfsavecontent tag to first save the contents in a variable and then we are writing the value of the variable to the sheet. The above method is much simpler and has an easy to understand code than that for the first method.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 6 and 2 and type the answer here:

User Comments

Title: instance, a FileOutputStream or ServletOutputStream   
Name: Lance
Date: 6/17/2009 3:00:53 PM
Comment:
Do have an example of how to use ServletOutputStream
Title: Reply: How do you specify sheetname   
Name: Debjani Mallick
Date: 6/11/2009 12:16:21 PM
Comment:
For specifying the arguments using the second write method, the syntax is:
writeExcelSheet('table name','datasource name','path to the excel sheet with sheet name');
This will create a sheet with the default name. Using the second write method, its first of all not possible to specify worksheet names and secondly, its also not possible to write to multiple tabs of an excel sheet through this method. But the first method specified, accomplishes both the tasks.
Title: How do you specify sheetname   
Name: jtansey
Date: 6/11/2009 9:19:14 AM
Comment:
So, in example two of writing to an excel worksheet, it says to specify the fileName as the full path along with the excelsheetname. I need to write to multiple sheets. How exactly do I specify the sheetname?
Title: Good one   
Name: Abx-68
Date: 5/7/2009 4:26:03 AM
Comment:
Good one!! Keep Going
Title: Helpful article   
Name: John
Date: 4/22/2009 11:15:24 AM
Comment:
Thank you for the article. It is really helping.

Product Spotlight
Product Spotlight 






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 4:39:45 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search