0.01
Low commit activity in last 3 years
A long-lived project that still receives updates
Rails Exporter (CSV, XML, XLS)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 0.21
~> 1.4

Runtime

~> 3.2
>= 4, < 8
~> 3.4
 Project Readme

Rails Exporter

Rails Exporter (CSV, XML, XLS, XSLX)

How to install

Add it to your Gemfile:

gem 'rails-exporter'

Run the following command to install it:

$ bundle install
$ rails generate rails_exporter:install

Generators

You can generate exporters app/exporters/example_exporter.rb

$ rails generate rails_exporter:exporter example

Generator will make a file with content like:

class ExampleExporter < RailsExporter::Base

  exporter do
    column :name
    column :email
    column :price => :currency
    column :is_admin => :boolean
    column :any_time => :time
    column :birthday => :date
    column :created_at => :datetime
    column(:account) {|record| record.account.name}
    column(:updated_at) do |record|
      record.updated_at.strftime("%d/%m/%Y %H:%M:%S")
    end
  end

  # exporter :simple do
  #   column :nome
  #   column :email
  # end

end

How to use

You can call export_to from Array or ActiveRecord::Relation objects:

    records = MyModel.all
    records.export_to(:csv) # or MyModelExporter.export_to_csv(records) 
    
    records = [MyModel.first, MyModel.last]
    records.export_to(:xml) # or MyModelExporter.export_to_xml(records)
    
    ## With context :simple
    # records.export_to(:csv, :simple)
    # or MyModelExporter.export_to_csv(records, :simple)

    ## CSV Custom Params
    # records.export_to(:csv, params: {col_sep: ','}))

Avaliable Types

You can call RailsExporter::Base.file_types to list all supported file types:

RailsExporter::Base.file_types.each do |type|
  puts type
end

I18n Configuration

Modify you config/locales/exporters.en.yml

Controller Example

class UsersController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.html
      format.csv { send_data @users.export_to(:csv) }
      format.xml { send_data @users.export_to(:xml) }
      format.xls { send_data @users.export_to(:xls) }
      format.xlsx { send_data @users.export_to(:xlsx) }
    end
  end
end

MimeTypes

Declare XLS as a new mimetype in config/initializers/mime_types:

    Mime::Type.register "application/vnd.ms-excel", :xls
    Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx