Roo gem allows us to access the contents ofOpen-office spreadsheets (.ods)
Following is a basic example to demonstrate how to read the datas from an excel sheet and save to your database.Suppose you have an excel sheet named which contain the student details such as “Name”,”Phone”,”Address”.We have to save them in our database but it will take a huge time if we insert them we can user “roo” gem to fetch the datas from excel sheet and save to database in a fly.
- Excel spreadsheets (.xls)
- Google (online) spreadsheets
- Excel’s new file format .xlsx
Step – 1
Add the gem in your gem file
Then run the ‘bundle install‘
Step – 2
Place your excel sheet in the rails application such as in public folder.
Step – 3
Now its time to change your controller like below.
- Add the following line in your controllerrequire ‘roo’
- Create a new method where you will fetch the data from excel sheet and save it in your database.def fetch_excel_dataex = ”)
ex.default_sheet = ex.sheets #Mention the sheet number
3.upto(1000) do |line| #start and end of row
name = ’)
phone = ’)
adr = ’)
@student = Student.create(:name => name,:phone => phone,:address => adr)