Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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_to do |format|
      format.html {}
      format.xlsx { render :xlsx => :index, :filename => "thingies_index" }
    end
  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{ xls_style: '{sz:14,format_code:"0000%"}' }= thingie.id
        %td= thingie.name
  %tfoot
    %tr
      %td(colspan="2") #{my_thingies.length}

See axlsx’s README for info and syntax on styles. If a TR has a style, it’s merged with any TD styles under it.

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

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages