Skip to main content
Inspiring
June 30, 2008
Question

export to .csv or better, .xls

  • June 30, 2008
  • 13 replies
  • 2394 views
Is there a way of exporting your table data to .csv format or better still .xls format ? We have approved users who need to save query results onto their own computers (in either .csv or .xls format)

Ideally the method used should be compatible across all common browsers?
This topic has been closed for replies.

13 replies

Inspiring
July 7, 2008
Can someone help with this last item - everything works fine in Mozilla Firefox, but when I test the code in IE7, instead of asking where to save the txt file, it fills the browser window with the contents of the txt file

I need it to ask where to save the file

Here's the code that appears to work in Firefox but not in IE7:

<cfheader name="Content-Disposition" value="inline; filename=flatfile.txt">
<cfcontent type="application/txt">
Participating Frequently
July 8, 2008
change
<cfheader name="Content-Disposition" value="inline; filename=flatfile.txt">
to
<cfheader name="Content-Disposition" value="attachment; filename=flatfile.txt">
July 7, 2008
I also deliver Excel spreadsheets to clients. But I place the data directly into the Excel spreadsheet and deliver the Excel spreadsheet.

I do this by starting with a standard Excel template. I copy this to another file name so that I can preserve the original template to use again. Then using ODBC I input the data directly into named areas on the spreadsheet. This allows a complete spreadsheet to be delivered with formulas, validations, and restrictions.
Inspiring
July 7, 2008
Thanks Ray, but all I needed was a simple output of any existing CF query to a csv, or as it turns out a tab delimited txt file.

The CF query I used is created programmatically, based on the search criteria the user chooses and stored in series of client variables

The index.cfm builds the query using the client variables and I have a link which spits out whatever database results are being shown.
Inspiring
July 7, 2008
Is it possible to construct the CSV using something other than a comma as a field delimiter ?

What else can I use ?
Inspiring
July 7, 2008
"Excel has detected its a SYLK file"
"Would you like to open it ?"

Clicking yes opens the file and it looks fine. So why is it doing the above ?
Inspiring
July 7, 2008
According to some pages I found via Google if the first two characters in your file are "ID" excel treats your file as a symbolic link (SYLK) file, whatever that is. You might try changing "ID" to "Record_ID" or "Model_ID".
Inspiring
July 7, 2008
Brilliant!! this solved it!

I still need to change the field delimiter though. What else may I use instead of a comma ?
Inspiring
July 7, 2008
ok so I have everything working with the code example below, but I need to add the field names to the first row of the csv. I've tried in the example below, but excel doesn't recognise the file format? Any ideas ?

<!--- following code spits out a .csv file --->

<cfsetting enablecfoutputonly="yes"> <!--- Required for CSV export to function properly --->
<cfset delim = 44> <!--- Use a comma for a field delimitter --->

<cfheader name="Content-Disposition" value="inline; filename=test.csv">
<cfcontent type="application/csv">

<!--- header showing column names--->
<cfoutput>ID#chr(delim)#manufacturer#chr(delim)#model#chr(delim)#sub_model#chr(delim)#ABS#chr(delim)#engine#chr(delim)#information#chr(delim)#gearbox#chr(delim)#year#chr(13)#</cfoutput>

<!--- Spill out data from a query --->
<cfloop query="getResults">
<cfoutput>#ID##chr(delim)##manufacturer##chr(delim)##model##chr(delim)##sub_model##chr(delim)##ABS##chr(delim)##engine##chr(delim)##information##chr(delim)##gearbox##chr(delim)##year##chr(13)#</cfoutput>
</cfloop>
Inspiring
July 7, 2008
What does the resulting file look like in a text editor? Are any additional line breaks being added where they are not expected?
Inspiring
July 3, 2008
Craig that's great news. I'll definitely give this a try.

I'm having a few issues at the moment with my clientstorge (DSN) so once I've got that done, I'll try what you suggest.

Big thanks!
Inspiring
July 3, 2008
Dax,

It would work for you. It was, from what I gather on Ben's blog, new to add CFMX 7 support. I use the utility on CF8 daily and no problems! Another bonus is that the utility has both a custom component AND a CFC. The do the same tasks but the CFC can be used on any CF installation (as opposed to the custom tag, which some ISPs do not allow -- of course, you can always throw the custom tag in the same directory as the calling CFML template but that's usually not as desired!).

Cheers,
Craig
Inspiring
July 2, 2008
Thanks craig - as I'm reliant on my ISP's CF8 server, do I take it this isn't for me ?

My ISP doesn't allow me to add custom tags to their server
The CF server is v8
Inspiring
July 2, 2008
Just in case folks didn't know Ben Nadel has a phenomenal utility called POI. It reads and creates XLS files from CF in a nice, clean, easy-to-use way. I've had great success with it on a few projects. Ben just updated it last month for CFMX 7. Might be worth checking out:
http://www.bennadel.com/projects/poi-utility.htm
Inspiring
June 30, 2008
incidentally I fixed the first code, by just putting the whole <cfoutput>.....</cfoutput> on the same line - this seemed to do the trick