to_xls Plugin: Export to Excel in Rails the Easy Way
January 10, 2009
Looking at the Google Analytics stats I realized that a lot of people are still reading my blog post about how to export to Excel in Rails.
IMO I prefer the csv method because it provides the same + more at no extra cost, but given people still wants xls files I wrote another rails plugin: to_xls.
UPDATE: SuperSaaS (Thanks!) mentioned that Excel does not interpret UTF8 characters in CSV files. So if that is a concern to you, maybe you should stick with XLS.
Using this plugin you don't need the builder views, so it makes it as easy as the to_csv plugin I wrote. In fact it works the same.
In config/initializers/mime_types.rb
register the custom mime type.
Mime::Type.register "application/vnd.ms-excel", :xls
In the controller where you want to export to excel, add the format.xls line.
class UserController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html
format.xml { render :xml => @users }
format.xls { send_data @users.to_xls }
end
end
end
That's it!
Install
./script/plugin install git://github.com/arydjmal/to_xls.git