|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
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[0]
user.password = row[1]
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[0]
user.password = row[1]
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[0]
user.password = row[1]
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 的意思是第一行存在行头,不导入数据库 |
|