分类:
2008-09-12 13:09:42
rails中使用fasterCSV解析csv文件比标准库的csv要快10倍左右 先安装fasterCSV 代码
gem install fastercsv
gem install fastercsv
gem install fastercsvViews: index.rhtml 代码
<% form_for :myform, :url=>{:controller=>"users", :action=>"import"}, :html => { :multipart => true } do |f| -%>
Select a CSV File :<%= f.file_field :file -%>
<%= submit_tag 'Submit' -%>
<% end -%>
<% form_for :myform, :url=>{:controller=>"users", :action=>"import"}, :html => { :multipart => true } do |f| -%>
Select a CSV File :<%= f.file_field :file -%>
<%= submit_tag 'Submit' -%>
<% end -%>
<% form_for :myform, :url=>{:controller=>"users", :action=>"import"}, :html => { :multipart => true } do |f| -%>
Select a CSV File :<%= f.file_field :file -%>
<%= submit_tag 'Submit' -%>
<% end -%>import.rhtml Java代码
OK!!!
OK!!!
OK!!!Controller: import Java代码
require 'faster_csv'
require 'faster_csv'
require 'faster_csv'Java代码
def import
n=0
FasterCSV.parse(params[:myform][:file],:headers=>true)do |row|
user = User.new
user.username = row[0]
user.password = row[1]
user.save!
n=n+1
GC.start if n%50==0 #GC 是Rails 的垃圾收集器的类(Garbage Collector,GC)
flash.now[:notice]="CSV Import Successful, #{n} new records added to data base"
end
end
def import
n=0
FasterCSV.parse(params[:myform][:file],:headers=>true)do |row|
user = User.new
user.username = row[0]
user.password = row[1]
user.save!
n=n+1
GC.start if n%50==0 #GC 是Rails 的垃圾收集器的类(Garbage Collector,GC)
flash.now[:notice]="CSV Import Successful, #{n} new records added to data base"
end
end
def import
n=0
FasterCSV.parse(params[:myform][:file],:headers=>true)do |row|
user = User.new
user.username = row[0]
user.password = row[1]
user.save!
n=n+1
GC.start if n%50==0 #GC 是Rails 的垃圾收集器的类(Garbage Collector,GC)
flash.now[:notice]="CSV Import Successful, #{n} new records added to data base"
end
end# :headers=>true 的意思是第一行存在行头,不导入数据库