Use of spreadsheet gem to integrate rails and excel.


Many a times,we run in a situation where we have to integerate our rails app and and excel worksheet.

Here is a simple step by step guide to do the same.

First step is to install a gem that I will be using.

The gem is called “Spreadsheet”.

The readme for the same can be found here

To install the gem use :

gem install spreadsheet.

(note : in rails 3 use gem ‘spreadsheet’ in the gem file and then do bundle install)

Once the gem is installed, one can open the controller action where the data is to be written to the ex cel file.

In my case the action was create.

The code for the same is :

def create       

 require ‘spreadsheet’                    

# include libraries

 @sample =[:sample])

      #sample is my model name


    respond_to do |format|


book =  

 # create a new excel file

sheet1 = book.create_worksheet      

#initialize a new sheet


#insert data in required sheet([row][column]) its starts from [0][0]

  book.write ‘C:/TC/test.xls’              # commit the write



(Note : written in blue are the comments and in black is the code)


Similarly to read data from excel files we use the same gem as:


def index

require ‘spreadsheet’                                   

#include the libraries.

  book = ‘C:/TC/test.xls’

#open the required file

  sheet1=book.worksheet 0

#open the required sheet


#read the data




One response to “Use of spreadsheet gem to integrate rails and excel.

  1. Pingback: Aunima-New Blog » rushabhthegodfather

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s