Coldfusion Export To Excel File
Tags:I was demonstrating to a co-worker the other day how you can easily create Microsoft Excel data files and stream them to the browser as an attachment that the end user can then open (in Excel) or save to the file system. This blew his mind, so I thought I would share it here as well.You don't need Java objects (ex. JExcel) or COM objects if you want to create simple Excel files. This technique uses basic HTML and just requests that it be interpreted by Microsoft Excel, not the user's browser. This does NOT create a full-fledged Excel document. When you open this document in Excel you will see that it does not have a proper grid.
However, if you copy and paste the Excel data into a new Excel sheet, it works flawlessly. For basic file creation, I consider this is to be completely acceptable (and so do all of my clients).To create the Excel file all you have to do is create a basic HTML page that caters to the Excel application way of doing things (such as using PT instead of PX for font sized, border widths, padding, etc) and then stream it to the browser with the proper CFHeader and CFContent tags. The CFHeader tag is always the same, but there are several ways to handle the content. You can stream it as text, as a file attachment, or as a binary data stream.
I suspect that the binary data stream and the file stream do pretty much the same thing, but as the 'Variable' attribute only came about in ColdFusion MX 7, I will demonstrate both methodologies.In the code below, also notice that I put in the ability to preview the code (?preview in URL). This has nothing to do with creating Excel files, this was just for debugging purposes and to demonstrate flexability.
Ben - nice example. I've been using the creating Excel file code provided by Nate Weiss in chapter 33 of CF MX7 Web Application Construction Kit.
His example code shows how to use a query's results to create tab-delimited output and then return the output to the user in such a way that the browser lets the user open the returned output in Excel. The data is lined up nicely in the grids in Excel.His code is quite short and has worked well for what we need. We often put 'Export to Excel' links on our pages to allow users to easily save the data to an Excel file on their computer.So your readers may also want to check out chapter 33. In that chapter are some other suggestions about creating output that can be read by Excel. Ben, this is probably the best article on CF-Excel creation on the web. I was trying to figure out how to generate comments on field column headers (those little yellow notepads that pop up).
Using your suggestion of exporting an excel sheet for the web revealed the proper code. A simple idea that hadn't crossed my mind. Thanks!For anyone else wanting to add comments:Export your example excel file and look for the XML data, that's all you need.
You don't need all the JS code that it generates unless you want the excel sheet to open as a HTML file.Daniel Elmore:).
MasRizal & Partners ColdFusion Developer - Reliable Outsource Partner Sharing Ideas And Solutions Downloadable Solutions PHP Classes For Small & Medium Enterprise Free For Non-Profit Organization Customer Feedback Contact Us Featured Web Template View More Templates MasRizal In Numbers. 20 years in business.
Coldfusion Export To Excel Example
6850 corporations in 67 countries have chosen our products and service. 73.24% are using more than one solutions from us.
Coldfusion Export To Excel File Download
MasRizal & Partners -Enhancing Business With Trust is available for immediate download. After registration, you will receive a confirmation email with your download link. Please ensure your email address is entered correctly during the evaluation process. Why Customers Choose Us Find out why our valued customers choose MasRizal & Partners solutions. Click here to see their reasons Featured Customers More than 6850 companies rely on MasRizal & Partners solutions. Click here to see our featured customers Free For Non-Profit Organization MasRizal Charity program allows eligible nonprofit organizations to get MasRizal's products at no cost.
Click here for program detail Testimonials Take the word of our clients Top Selling Products See our top selling products Let Us Help You Can't find solution you need? Click here to get solution from MasRizal.Sharing Ideas And Solutions.
Is available for immediate download.After registration, you will receive a confirmation email with your download link.Please ensure your email address is entered correctly during the evaluation process.Why Customers Choose UsFind out why our valued customers choose MasRizal & Partners solutions.Featured CustomersMore than 6850 companies rely on MasRizal & Partners solutions.Free For Non-Profit OrganizationMasRizal Charity program allows eligible nonprofit organizations to get MasRizal's products at no cost.TestimonialsTop Selling ProductsLet Us Help YouCan't find solution you need?