When I import an excel data file is saving format as number.
Excel example
|num_doc| |name|
1234 CR7
5678 Beckham
9102 Bush
Controller client_controller.rb:
def import
Client.import(params[:file])
end
Model client.rb
@errors = []
spreadsheet = open_spreadsheet(file)
(2..spreadsheet.last_row).each do |i|
column1 = spreadsheet.cell(i,'A').to_s
column2 = spreadsheet.cell(i,'B').to_s
client = Client.new(:num_doc => column1,
:name => column2)
if client.save
# stuff to do on successful save
else
client.errors.full_messages.each do |message|
@errors << "Issue on line #{i}, <strong>column #{message}</strong>".html_safe
end
end
end
@errors # <- need to return the @errors array
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Csv.new(file.path, nil, :ignore)
when ".xls" then Excel.new(file.path, nil, :ignore)
when ".xlsx" then Excelx.new(file.path, nil, :ignore)
else raise "Excel issue: #{file.original_filename}"
end
end
When I import the excel file the column num_doc is saving adding number format and want to save the string value.
|num_doc| |name|
1234.0 Cr7
5678.0 Beckham
I tried but is still saving as number.
column1 = spreadsheet.cell(i,'A').to_s #to String
:num_doc => column1.to_s #to string