Skip to content

Instantly share code, notes, and snippets.

@mallorydxw
Created December 2, 2013 19:46
Show Gist options
  • Save mallorydxw/7756841 to your computer and use it in GitHub Desktop.
Save mallorydxw/7756841 to your computer and use it in GitHub Desktop.
Apparently converting XLSX to CSV and keeping formulae intact is not something you can Google and find out how to do it? This is how you do it.
#!/usr/bin/env ruby
require 'rubygems'
require 'csv'
require 'roo'
excel = Roo::Excelx.new('filename.xlsx')
CSV do |csv| # (stdout)
excel.sheets.each do |s|
sheet = excel.sheet(s)
1.upto(excel.last_row) do |y|
row = []
1.upto(excel.last_column) do |x|
if sheet.formula?(y, x)
row << '=' + sheet.formula(y, x)
else
row << sheet.cell(y, x)
end
end
csv << row
end
csv << ['----------']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment