data into excel

I was wondering if there is a way to create an excel file with ROR..?

Here is what i have:
The app i have has a reports section, that section has many reports that
use the find_by_sql in order to customize the query. On the main reports
page the use is able to click and a report link and it opens up a new
window displaying the data i want to be able to have a link that when
clicked it creates an excel file that the user can download.

Is there any way to accomplish that..?

If you'll settle for a CSV that will open up in Excel, then it's pretty easy... here's an example out of our site

entry = VideoGalleryEntry.find_by_id(params[:id])
Tempfile.open("video-gallery-report-#{entry.id}") do |t|
     CSV::Writer.generate(t, ',') do |csv|
     csv << ["Date", "Number of Views"]
     entry.views.sort {|a,b| a.viewed_on <=> b.viewed_on }.each do |v|
       csv << [v.viewed_on, v.views]
     end
   end
   t.size
   t.rewind
   send_data(t.read, :type => 'application/vnd.ms-excel; charset=iso-8859-1; header=present',
                     :filename => "video-gallery-report-#{entry.id}.csv")
end

Don’t forget that there’s always the option of just rendering an HTML table but reporting the content-type as application/vnd.ms-excel .

Then when someone wants a .xls document you just render an html table without a layout and Excel (as well as OpenOffice) will open the file up as a single-sheet excel document. I’ve yet to get this method to work with Google Spreadsheets or Gnumeric though.

First register the MIME type in environment.rb:
Mime::Type.register “application/vnd.ms-excel”, :xls, [ “application/excel”, “application/x-excel” ]

Then you can just have a respond_to block with wants.xls? rendering with no layout. Provided you are setup in such a way that the convention of this works for you. It might take a little more work in your own application.

Taylor Singletary
Reality Technician

http://www.realitytechnicians.com