用fasterCSV快速处理导入CSV文件
rails中使用fasterCSV解析csv文件比标准库的csv要快10倍左右 先安装fasterCSV Java代码gem install fastercsv
gem install fastercsv
gem install fastercsvViews: index.rhtml Java代码
<% 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代码
<font color=red>OK!!!</font>
<font color=red>OK!!!</font>
<font color=red>OK!!!</font>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
user.password = row
user.save!
n=n 1
GC.start if nP==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
user.password = row
user.save!
n=n 1
GC.start if nP==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
user.password = row
user.save!
n=n 1
GC.start if nP==0 #GC 是Rails 的垃圾收集器的类(Garbage Collector,GC)
flash.now[:notice]="CSV Import Successful, #{n} new records added to data base"
end
end# :headers=>true 的意思是第一行存在行头,不导入数据库
页:
[1]