熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用fasterCSV快速處理導入CSV文件

2022-06-13   來源: Java核心技術 
      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
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.