parm530
10/15/2019 - 2:46 PM

CSV Loading

Import the CSV

  • Place the csv into public/import directory
  • Use the roo gem to help with reading the .csv or .xlsx file
  • In your code:
up_file = Roo::Spreadsheet.open("{Rails.root}/public/import/file_name.xlsx")

# set the defaul sheet
up_file.default_sheet = up_file.sheets[0]

# get the header
header = up_file.row(1)

# looping through each row in the sheet
# start at 2, since the 1st row was the header
(2..up_file.last_row).each.with_index(1) do |el, index|
  row_data = Hash[[header, up_file.row(i)].transpose]
  
  # able to access the row info
  
end