Simple Office 2007 Excel file to CSV hack

I needed to convert a very simple Office 2007 Excel file into a CSV. I don’t have Office 2007 so I hacked up some Ruby goodness to convert it.


require 'rubygems'
require 'hpricot'
doc = open('office2007.xls') { |f| Hpricot(f) }
csv = ''
doc.search('//row').each do |row|
(row/'cell/data').each do |data|
csv < < "\"#{data.to_plain_text}\"," if data
end
csv.chomp!(',')
csv << "\n"
end
File.open("output_file_name", 'w') {|f| f.write(csv) }

Hope someone finds this useful.

Found this interesting? Share it:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • TwitThis

Leave a Reply