江西广告网
标题:
用fasterCSV快速处理导入CSV文件
[打印本页]
作者:
monicaken
时间:
2009-2-12 09:00
标题:
用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[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 的意思是第一行存在行头,不导入数据库
欢迎光临 江西广告网 (http://bbs.jxadw.com/)
Powered by Discuz! X3.2