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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<code lang="ruby">
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) }
</code>
</code> |
Hope someone finds this useful.