A gem for Ruby on Rails for writing Microsoft excel files
Ruby
Latest commit 567492d Jan 2, 2012 @hallelujah Bump to 1.0.3
Permalink
Failed to load latest commit information.
lib Bump to 1.0.3 Jan 2, 2012
.gitignore Ignoring swap files Aug 22, 2011
.rvmrc Use ree-1.8.6 Aug 22, 2011
Changelog Reflect changes Jan 2, 2012
Gemfile Import skel Aug 22, 2011
README.md Fix typo readme Jan 2, 2012
Rakefile Add gem dependencies + tasks Aug 22, 2011
TODO.md Update todo Jan 2, 2012
rails-excel.gemspec Remove builtin strategies : new gems exist Jan 2, 2012

README.md

Rails Excel

It adds support of .rxls templates for your rails views

[DEPRECATED]

Prior to 1.0.0, it comes with two builtin strategies based on the very good gems writeexcel and spreadsheet

[IMPORTANT]

As of 1.0.0, there is no builtin strategy anymore. You should install at least one strategy :

Requirements

  • rails >= 2.3.0
  • ruby >= '1.8.6'

Usage

In your config/environment.rb

config.gem 'rails-excel'

Create an initializer : config/initializers/excel.rb

RailsExcel.configure do |config|
  config.strategy = :spreadsheet # by default or :write_excel
end

If you wan tot implement your own strategy, here are the requirements :

  • Must respond to compile
  • First argument of compile takes a StringIo instance where to write the response
  • compile takes a block that yields a workbook instance

Example extracted from code source:

# lib/my_strategy.rb

class MyStrategy
  def compile(io, &block)
    workbook = ::Spreadsheet::Workbook.new
    yield(workbook)
    workbook.write(io)
  end
end

Then in your config/initializers/excel.rb

require 'my_strategy'
RailsExcel.configure do |config|
  config.add_strategy :my_strategy, MyStrategy.new
  # Redefining default strategy
  config.strategy = :my_strategy # by default it was :spreadsheet
end

You can use any object as long as it responds to described compile method

The strategy defined in the initializer will be used in all of your controllers

To use another strategy you can set it by controller :

class UsersController < ApplicationController
  self.excel_strategy = :write_excel
end

Or you can set strategy per action by redefining instance method excel_strategy

class UsersController < ApplicationController
  self.excel_strategy = :write_excel
  def index
    # ...
  end

  def show
    # ...
  end

  protected
  def excel_strategy
    case action_name
    when 'index'
      :spreadsheet
    else
  self.class.excel_strategy
    end
  end
end

Or you can also override excel_strategy by action like that :

class UsersController < ApplicationController
  self.excel_strategy = :write_excel

  def other_action
    self.excel_strategy = :rubyXL
    # ...
  end

end

Contributors