Skip to content

Instantly share code, notes, and snippets.

@amaunz
Created July 2, 2012 12:35
Show Gist options
  • Save amaunz/3033018 to your computer and use it in GitHub Desktop.
Save amaunz/3033018 to your computer and use it in GitHub Desktop.
Convert XLS to CSV
# Generate a csv from xls using ruby with a HERE document
# uses ruby's 'roo' gem
# Creates output for every sheet separately
# Usage: xls2csv </path/to/xlsfile.xls>
xls2csv () {
if [ ! -f $1 ]; then
echo "File '$1' not found."
exit 1
fi
ruby <<EOF
require 'rubygems'
require 'roo'
file = "$1"
xls = Excel.new(file)
xls.sheets.each { |s|
xls.default_sheet = s
xls.to_csv("#{s}.csv")
}
puts file
EOF
}
@sidharthanc
Copy link

Thanks for this code for converting excel to csv..but two questions i have

  1. can we specify the separator for generating csv?
  2. in this code when converting excel to csv its changing the date format (eg: there is field dob in excel it was mm/dd/yyyy) but in the generated csv its showing yyyy-mm-dd.. can we generate the values in csv without changing the formats.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment