从 Excel 文件中读取和保存数据 - 如何解析文件 - #<Array:0x1579b6a8> 的未定义方法“to_f”
Reading and saving data from Excel file- How to parse a file - undefined method `to_f' for #<Array:0x1579b6a8>
我遵循了 RailsCast 上关于如何从 Excel 导入数据的教程。
我想阅读我的 excel 文件并告诉他要阅读和保存哪些信息以指示要提取的行。所以我在我的模型(.rb)文件中写了类似的东西:
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv<<column_names
all.each do |repartition|
csv<<repartition.attributes.values_at(*column_names)
end
end
end
def self.import(file)
#Ouverture du fichier
spreadsheet = open_spreadsheet(file)
#Lecture du fichier, lignes par lignes, en aveugle
(1..spreadsheet.last_row).each do |i|
@repartition=Repartition.new
@repartition.fond_repartition_id=spreadsheet.row(4)
@repartition.date_repartition=spreadsheet.row(3)
@repartition.AssetAllocCash=spreadsheet.row(5)
@repartition.AssetAllocEquity=spreadsheet.row(6)
@repartition.AssetAllocBond=spreadsheet.row(7)
@repartition.AssetAllocOther=spreadsheet.row(8)
#Sauvegarde de la ligne qui vient d'être lu
repartition.save!
#Depart prochaine ligne ou fin
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, options={})
when ".xls" then Roo::Excel.new(file.path, options={})
when ".xlsx" then Roo::Excelx.new(file.path, options={})
else raise "Unknown file type: #{file.original_filename}"
end
end
然后我在我的控制器中写了这段代码
def import
Repartition.import(params[:file])
redirect_to upload_path, notice: "Répartition effectuée avec succès"
end
我收到错误消息:
NoMethodError in RepartitionsController#import
undefined method `to_f' for #<Array:0x1579b6a8>
我所有的数据都是浮动属性。为什么我有这个错误?
这对我有用。
为了解析我的 excel 文件,我执行了以下步骤。
我正在使用 gem roo。所以在我的 GemFile 中有一行:
gem 'roo', '~>2.1.0'
然后我运行命令行:
bundle install
在我的控制器中:
def import
Repartition.import(params[:file])
redirect_to resultat_path, notice: "Répartition effectuée avec succès"
end
然后在我的模型中:
def self.open_file(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, options={})
when ".xls" then Roo::Excel.new(file.path, options={})
when ".xlsx" then Roo::Excelx.new(file.path, options={})
else raise "Unknown file type: #{file.original_filename}"
end
end
def self.import(file_path)
#Ouverture du fichier
file = open_file(file_path)
#Lecture du fichier
(1..file.last_row).each do |i|
row = file.row(i)
@repartition=Repartition.new
@repartition.fond_repartition_id=row[4]
@repartition.date_repartition=row[3]
@repartition.save!
end
end
然后调用方法,在我要交互的网页上,我写了这样的:
<%= form_tag import_repartitions_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Importer", class: "btn btn-primary" %>
<% end %>
错误是忘记写了(首先)
row = file.row(i)
然后我试图阅读的内容有错误的名称。在我的数据库模式中,我迁移了我的 table 属性 "date_repartitio" 而不是 "date_repartition"
错误 to_f 说的不是读取 "date_repartition" 这是一个浮点数(不是我知道的日期时间)而是读取 "date_repartitio" 这是实际上什么都不存在。
但是我还没有代码来避免每次 运行 导入方法时都创建一个实体。
我遵循了 RailsCast 上关于如何从 Excel 导入数据的教程。
我想阅读我的 excel 文件并告诉他要阅读和保存哪些信息以指示要提取的行。所以我在我的模型(.rb)文件中写了类似的东西:
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv<<column_names
all.each do |repartition|
csv<<repartition.attributes.values_at(*column_names)
end
end
end
def self.import(file)
#Ouverture du fichier
spreadsheet = open_spreadsheet(file)
#Lecture du fichier, lignes par lignes, en aveugle
(1..spreadsheet.last_row).each do |i|
@repartition=Repartition.new
@repartition.fond_repartition_id=spreadsheet.row(4)
@repartition.date_repartition=spreadsheet.row(3)
@repartition.AssetAllocCash=spreadsheet.row(5)
@repartition.AssetAllocEquity=spreadsheet.row(6)
@repartition.AssetAllocBond=spreadsheet.row(7)
@repartition.AssetAllocOther=spreadsheet.row(8)
#Sauvegarde de la ligne qui vient d'être lu
repartition.save!
#Depart prochaine ligne ou fin
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, options={})
when ".xls" then Roo::Excel.new(file.path, options={})
when ".xlsx" then Roo::Excelx.new(file.path, options={})
else raise "Unknown file type: #{file.original_filename}"
end
end
然后我在我的控制器中写了这段代码
def import
Repartition.import(params[:file])
redirect_to upload_path, notice: "Répartition effectuée avec succès"
end
我收到错误消息:
NoMethodError in RepartitionsController#import
undefined method `to_f' for #<Array:0x1579b6a8>
我所有的数据都是浮动属性。为什么我有这个错误?
这对我有用。 为了解析我的 excel 文件,我执行了以下步骤。
我正在使用 gem roo。所以在我的 GemFile 中有一行:
gem 'roo', '~>2.1.0'
然后我运行命令行:
bundle install
在我的控制器中:
def import
Repartition.import(params[:file])
redirect_to resultat_path, notice: "Répartition effectuée avec succès"
end
然后在我的模型中:
def self.open_file(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, options={})
when ".xls" then Roo::Excel.new(file.path, options={})
when ".xlsx" then Roo::Excelx.new(file.path, options={})
else raise "Unknown file type: #{file.original_filename}"
end
end
def self.import(file_path)
#Ouverture du fichier
file = open_file(file_path)
#Lecture du fichier
(1..file.last_row).each do |i|
row = file.row(i)
@repartition=Repartition.new
@repartition.fond_repartition_id=row[4]
@repartition.date_repartition=row[3]
@repartition.save!
end
end
然后调用方法,在我要交互的网页上,我写了这样的:
<%= form_tag import_repartitions_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Importer", class: "btn btn-primary" %>
<% end %>
错误是忘记写了(首先)
row = file.row(i)
然后我试图阅读的内容有错误的名称。在我的数据库模式中,我迁移了我的 table 属性 "date_repartitio" 而不是 "date_repartition" 错误 to_f 说的不是读取 "date_repartition" 这是一个浮点数(不是我知道的日期时间)而是读取 "date_repartitio" 这是实际上什么都不存在。
但是我还没有代码来避免每次 运行 导入方法时都创建一个实体。