• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 16 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This Rails plugin is the easiest way to export to Excel. It gives you the ability to call to_xls to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.

to_xls pluginΒΆ ↑

This Rails plugin is the easiest way to export to Excel. It gives you the ability to call to_xls to a collection of activerecords (Array). The builder options are the same as to_json / to_xml, except for the :include.

NOTE: IMO I would use the to_csv plugin; it’s more compatible. (github.com/arydjmal/to_csv)

UsageΒΆ ↑

@users = User.all

#
# defaults are export headers and all fields
#

@users.to_xls
@users.to_xls(:only => [:last_name, :role])
@users.to_xls(:headers => false)
@users.to_xls(:except => [:last_name, :role])
@users.to_xls(:methods => :admin?)

Real life exampleΒΆ ↑

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

  def show...
  def new...
  def edit...
  def create...
  def update...
  def destroy...

end

DependenciesΒΆ ↑

None.

InstallΒΆ ↑

./script/plugin install git://github.com/arydjmal/to_xls.git

NoteΒΆ ↑

Does not work on a single activerecord, ie, User.first.to_xls.

Copyright Β© 2009 Ary Djmal, released under the MIT license