rails 上读取csv文件 (来自stackoverflow)
今天要做读取csv文件里面的内容,本来项目里面有代码,可是找了半天就是没有找到。在网上找了一份。
前端代码
<%= form_for :mytest, :html =>{:multipart => true} do |f| %> <%= f.file_field :myfile %> <%= f.submit "Upload" %> <% end %>
后端代码:
require 'csv' require 'fileutils' def upload tmp = params[:mytest][:myfile] current_file_name = tmp.original_filename + Time.now.to_i.to_s //保存 file = File.join("public/upload_file", current_file_name) FileUtils.cp tmp.path, file //读取csv文件 csv_text = File.read("public/upload_file/" + current_file_name) csv = CSV.parse(csv_text,:headers => true) csv.each do |row| puts (row.to_hash).to_s end end
搞定。
相关推荐
wl00 2020-10-28
EricNet 2020-07-05
EricNet 2020-05-27
何志文 2020-05-11
JOO 2020-04-26
happyfreeangel 2020-04-09
Poisedflw 2020-03-23
yangliuhbhd 2020-03-06
Ben的程序员生涯 2013-06-01
chenshuixian 2013-06-01
wes0 2014-05-31
mrice00 2019-12-20
EricNet 2019-12-11
89304896 2019-12-08
lihaoningxia 2013-07-09
userguanguan 2015-03-16