Seeding database from .XLS file

/ Published in: Ruby
Save to your folder(s)

I had a spreadsheet with a ton of data on it that I wanted in my seeds file. Using example columns in your database titled db_column1, db_column2, db_column3. Place your .xls file into your public folder and run 'gem install roo'

Copy this code and paste it in your HTML
  1. require 'roo'
  3. def fetch_excel_data
  4. ex ="public/your_spreadsheet.xls")
  5. ex.default_sheet = ex.sheets[0] #Mention the sheet number (0 is the first sheet, 1 is second sheet, etc.)
  6. 2.upto(500) do | line | #Start and end of rows you want to include
  7. db_column1 = ex.cell(line,'A') #Column A in spreadsheet
  8. db_column2 = ex.cell(line,'B')
  9. db_column3 = ex.cell(line,'C')
  11. x = Class.create(:db_column1 => db_column1, :db_column2 => db_column2, :db_column3 => db_column3)
  13. end

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.