IMPORTING CSV FILE DATA IN RAILS

We would like to import data from a CSV file into an existing database table. I do not want to save the CSV file, just take the data from it and put it into the existing table.

We need to put the gem in your gemfile:-

gem 'roo'

def import
            file = "Desktop/rohit.csv"
           # or
          file = "Desktop/rohit.xlsx"    # you can import any csv or xlsx file
                 
          doc = Roo::Excelx.new(file, nil, :ignore)
          header = doc.row(1)          # if we need header
          (2..doc.last_row).each do |i|
              row = Hash[[header, doc.row(i)].transpose]
              puts "#{row["id"]}"
          end 
end
Post a Comment

Popular posts from this blog

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER