Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
importing data from excel file into mysql database
3 Posts
importing data from excel file into mysql database

how can i import data from my csv file to mysql is there any solution using fasterCSV

require 'win32ole' def import_xls

  if (params[:button1] == "Excel TO MySQL" )
   puts "im in"
           file=params[:document][:file].read 
    # file="C:\\Documents and Settings\\Administrator\\Desktop\\Excel1.xls"
     puts file  
     application = WIN32OLE.new("Excel.Application")
     worksheet = application.Workbooks.Open(file).Worksheets("Sheet1")
     worksheet.Activate  
   begin      
   row =1 
   column =1 
    while(row  'index' 

end # end of import_xml

----------------------------------------- END OF IMPORTING -----------------

Here's a sample that I use:


require 'fastercsv'

class Account < ActiveRecord::Base

  Name       = 2
  Created_At = 1
  Email      = 13
  Contact    = 12
  DOB        = 17
  Balance    = 6
  
  def self.create_from_csv(csv_file)
    FasterCSV.parse(csv_file.read, :headers => true).each do |row|
      account = find_or_create_by_name row[Name]
      
      account.update_attributes(:created_at => row[Created_At],
                                :email      => row[Email],
                                :contact    => row[Contact],
                                :dob        => row[DOB])

      account.transactions.create(:amount => row[Balance],
                                  :date   => Date.today,
                                  :notes  => 'Imported balance')
    end
  end
end

3 Posts
Login to add your message