我的系统中有.csv文件,我想导入到openproject中,并将文件内容映射到 openproject数据库。
答案 0 :(得分:0)
过去我必须将CSV文件加载到DB中我喜欢执行rake任务。
我的任务如下:
#/lib/tasks/import_csv.rake
require 'csv'
namespace :imports do
desc 'import from csv'
task :variants => :environment do
Dir['lib/csv/files/*'].each do |file|
csv_text = File.read(file)
csv_text = csv_text.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row, index|
something = Something.find_or_initialize_by(name: row['name'])
something.address = row['address']
something.save
end
file_processed = file.gsub('/files/', '/files_processed/')
File.rename(file, file_processed) unless Rails.env == 'development'
end
end
end
我希望这会有所帮助:)