Skip to content

Instantly share code, notes, and snippets.

@xiejiangzhi
Last active November 27, 2018 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiejiangzhi/5464fa803e4260bde54b919c7e9219a3 to your computer and use it in GitHub Desktop.
Save xiejiangzhi/5464fa803e4260bde54b919c7e9219a3 to your computer and use it in GitHub Desktop.
Convert excel(xls, xlsx, etc) to csv
#! /usr/bin/env ruby
# Support formats
# xlsx: Excel 2007 - 2013
# xlsm: Excel 2007 - 2013
# xls: Excel 97,
# ods: LibreOffice / OpenOffice.org
# xml: Excel 2002 2003 XML
#
# More see roo-rb/roo
require 'csv'
require 'roo'
require 'roo-xls'
path, sheet = ARGV.first.split(':')
puts "not found file" unless File.exists?(path)
xlsx = Roo::Spreadsheet.open(path)
str = CSV.generate do |csv|
xlsx.sheet(sheet.to_i).to_a.each { |row| csv << row }
end
puts str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment