Important alert: (current site time 5/22/2013 9:47:32 AM EDT)
 

VB icon

Write a Excel file with Query output

Email
Submitted on: 10/4/2001 1:00:21 PM
By: Tim Garver  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Cold Fusion 4.5
Views: 26874
author picture
(About the author)
 
     If you are like me, my bosses all ask for reports. This is how I give them the reports. I use an advanced query to pull the appropriate data then write it all in an excel spread sheet. This uses the CFFILE tag to write the spread sheet file. Granted it comes out as a CSV file, but Excel will open it. If your host has disallowed CFFILE, check out my other post titled "HTML to EXCEL"
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Write a Excel file with Query output
//**************************************
as is. no claims. use as you wish.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Write a Excel file with Query output
// Description:If you are like me, my bosses all ask for reports. This is how I give them the reports. I use an advanced query to pull the appropriate data then write it all in an excel spread sheet.
This uses the CFFILE tag to write the spread sheet file. Granted it comes out as a CSV file, but Excel will open it. If your host has disallowed CFFILE, check out my other post titled "HTML to EXCEL"
// By: Tim Garver
//
// Inputs:All the colums from your query.
//
// Returns:Returns a text CSV file that can be opened in Excel.
//
// Assumes:You must have CFFILE enabled on your server, or this will cause an error.
If you do not have this capability, take a look at my other post titled "HTML to Excel" its much easier..
//
// Side Effects:Make sure you fix your data so that no ","'s are in the data. This will cause your colums to shift over. I use the #REPLACE()# function to rip out commas.
You will also want to surround any numeric fields that start with a leading Zero with single qoutes.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=27&lngWId=9//for details.//**************************************

<cfquery name="daily" datasource="#mydatasource#" dbtype="ODBC" >
SELECT *
FROM Customers
Where company <> ''
</cfquery>
<!--- make some variables to store the dirctory and file name. --->
<cfset f_dir = "C:\reports\">
<!--- I like to use the date and time for a file name, but you can name it anything you like by changing the value below. --->
<cfset f_name = "#dateformat(now(), 'mmddyy')##timeformat(now(), 'hhmm')#.csv">
<!--- Lets make the file, and put the first row of Column headings in --->
<cffile action="WRITE" file="#f_dir##f_name#"
 output="Company, Sales Area, Sales Person, Site Contact, Contact Phone, Contact Email, Contact Title" addnewline="Yes">
<!--- Now lets loop over the RecordSet and fill in the data --->
<cfloop query="daily">
<cffile action="APPEND" file="#f_dir##f_name#" 
output="#REPLACE(company, ",", "","AlL")#, #region#, #salesperson#, #firstname# #lastname#, #contactphone# #contactphoneext#, #contactsemail#, #contactsTitle#" 
addnewline="Yes">
<!--- End the loop here --->
</cfloop>
<br>
< a href="/reports/#f_name#">Here is the file</a>


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/4/2002 4:16:32 AMGirish

The Ecel file gets created but the data is clubbed in only one tab with comma seperated values.I want the data to be in different tabs.

For Ex:-The Name and the Age os coming in one tab as Name,Tab whereas the requriement was Name in one tab and Age in another tab.

Please advice.

Thanks & Regards,
Girish
(If this comment was disrespectful, please report it.)

 
9/27/2003 1:43:29 AMTim Garver

In this line above:
"output="#REPLACE(company, ",", "","AlL")#, "
Use this same technique on any field that may contain a COMMA.

If a variable contains a comma when out put into a csv file, it causes a new cell to be created. So you can either find and replace all commas with double quote comma double quote for each comma found or with a space. This will eliminate that issue.
(If this comment was disrespectful, please report it.)

 
7/4/2008 8:18:55 AMJameson Iniobong Bolaji

nice code
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.