0

The date column in spreadsheet is formatted as 'mm/dd/yyyy' But when I read the above column via spreadsheet the format is different than the above format.

My code is as follows:

require 'spreadsheet'

Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open params[:excel_file]
sheet1 = book.worksheet 0
sheet1.each do |row|
  row.set_format 1, Spreadsheet::Format.new(:number_format => 'MM/DD/YYYY')
  h = Hash.new
  h["name"] = row[0]
  h["date"] = row[1]
  ......
end

Currently h["date"] is not retrieving properly. So How should I retrieve the date column properly with my original formatting. Can anyone help me to sort this out !

diya
  • 6,938
  • 9
  • 39
  • 55

1 Answers1

0

row[1].date or row[1].datetime will get you the right value. I suppose you can set the format while writing it to excel not the other way round.

Update
I thought you just needed your excel date translated as ruby date.
This should do the trick:
excel_date = row[1].date
ruby_date = excel_date.strftime('%m/%d/%Y')

rb512
  • 6,880
  • 3
  • 36
  • 55