Skip to content

pivotalhalogen/to_spreadsheet

 
 

Repository files navigation

to_spreadsheet is a gem that lets you render xls from your existing haml/erb views from Rails (>= 3.0). Build Status

Installation

Add it to your Gemfile:

gem 'to_spreadsheet'

Usage

In your controller:

# my_thingies_controller.rb
class MyThingiesController < ApplicationController
  respond_to :xls, :html
  def index
    @my_thingies = MyThingie.all
    respond_with(@my_thingies)
  end
end

In your view partial:

# _my_thingie.haml
%table
  %caption My thingies
  %thead
    %tr
      %td ID
      %td Name
  %tbody
    - my_thingies.each do |thingie|
      %tr
        %td.number= thingie.id
        %td= thingie.name
  %tfoot
    %tr
      %td(colspan="2") #{my_thingies.length}

In your index.xls.haml:

# index.xls.haml
= render 'my_thingies', :my_thingies => @my_thingies

In your index.html.haml:

# index.html.haml
= link_to 'Download XLS', my_thingies_url(:format => :xls)
= render 'my_thingies', :my_thingies => @my_thingies

Formatting

You can use class names on td/th for typed values. Here is the list of class to type mapping:

CSS class Format
decimal or float Decimal
num or int Integer
datetime DateTime
date Date
time Time

Worksheets

Every table in the view will be converted to a separate sheet.
The sheet title will be assigned to the value of the table’s element if it exists.

About

Render XLS from Rails using existing views

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%