Skip to content

Instantly share code, notes, and snippets.

@hahaliu005
Created April 9, 2015 10:02
Show Gist options
  • Save hahaliu005/2daf8cb334ae02c46232 to your computer and use it in GitHub Desktop.
Save hahaliu005/2daf8cb334ae02c46232 to your computer and use it in GitHub Desktop.
Read excel file use ruby, xls or xlsx file
# gem install roo -V
# maybe you will occur an error when install roo,
# because psych the lastest version no longer has the class2path method
# downgrading the Psych gem to version 2.0.0 can fix this error
# gem uninstall psych -v 2.0.5
#
require 'roo'
# this must requied
require 'uri'
# the file can be XX.xls or XX.xlsx, It's all be fine
xlsx = Roo::Spreadsheet.open('./tc_award.xls')
xlsx.sheets.each do | sheet_name |
xlsx.sheet(sheet_name).each(id: 'INT_id', award: 'STR_awards') do | hash |
p hash.inspect
# eg: "{:id=>5.0, :award=>\"[{item,{1201,1}}]\"}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment