rails中使用fasterCSV解析csv文件比標准庫的csv要快
倍左右 先安裝fasterCSV Java代碼
gem install fastercsv
gem install fastercsv
gem install fastercsvViews: indexrhtml Java代碼
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>importrhtml 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_csvJava代碼
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end# :headers=>true 的意思是第一行存在行頭不導入數據庫
gem install fastercsv
gem install fastercsv
gem install fastercsvViews: indexrhtml Java代碼
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>
<% form_for :myform :url=>{:controller=>users :action=>import} :html => { :multipart => true } do |f| %>
Select a CSV File :<%= ffile_field :file %>
<%= submit_tag Submit %>
<% end %>importrhtml 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_csvJava代碼
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end
def import
n=
FasterCSVparse(params[:myform][:file]:headers=>true)do |row|
user = Usernew
userusername = row[]
userpassword = row[]
usersave!
n=n+
GCstart if n%== #GC 是Rails 的垃圾收集器的類(Garbage CollectorGC)
flashnow[:notice]=CSV Import Successful #{n} new records added to data base
end
end# :headers=>true 的意思是第一行存在行頭不導入數據庫
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25529.html