Importing CSV and Excel in rails 4 and ruby 2.0

You can use a single gem ‘roo’ for reading all types of file, instead of rubyXL, spreadsheet etc. multiple gems for different files(.xls, .xlsx).
You can check here railscasts for importing csv and excel import in ruby 1.9.3.

But this code snippet is not working with Ruby 2.0.
As this episode used gem ‘roo’ for reading files and gem ‘roo’ has been updated for rails 4.0.
So you need to do some code changes to work it with rails 4.0

Changes are:
1. In Method: self.open_spreadsheet(file)

Csv.new(file.path, nil, :ignore) => Roo::Csv.new(file.path, nil, :ignore)
Excel.new(file.path, nil, :ignore) => Roo::Excel.new(file.path, nil, :ignore)
Excelx.new(file.path, nil, :ignore) => Roo::Excelx.new(file.path, nil, :ignore)

2. Remove

require ‘iconv’

from config/application.rb

For more about roo gem click here

Advertisements

One thought on “Importing CSV and Excel in rails 4 and ruby 2.0

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s