-1
我在我的系统中有一个.csv文件,并且我想将导入即打开项目并将文件内容映射到openproject数据库。映射.csv文件打开项目数据库
我在我的系统中有一个.csv文件,并且我想将导入即打开项目并将文件内容映射到openproject数据库。映射.csv文件打开项目数据库
在过去,当我不得不将一个CSV文件加载到数据库我喜欢todo 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
我希望这有助于:)
应在哪个地方存在.csv? – Ajay
什么是你的问题? – Ilya
llya,我想将一个.csv文件导入openproject数据库。总之,我想创建一个导入插件 – Ajay
到目前为止你尝试了什么? – Ilya