Skip to content

Instantly share code, notes, and snippets.

@callumj
Created January 4, 2017 18:15
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 callumj/7f9aca229f1df01a94a40e1195293018 to your computer and use it in GitHub Desktop.
Save callumj/7f9aca229f1df01a94a40e1195293018 to your computer and use it in GitHub Desktop.
Collate multiple CSV into sheets in one Excel workbook using RubyXL
files = Dir.glob("#{ENV["FOLDER"]}/**.csv").sort
files.each do |file|
worksheet = workbook.add_worksheet(file.gsub("#{ENV["FOLDER"]}/", "").gsub(".csv", "").gsub("*", ""))
row_indx = 0
CSV.foreach(file) do |row|
row.each_with_index do |cell, cell_indx|
worksheet.add_cell(row_indx, cell_indx, cell)
end
row_indx += 1
end
end
workbook.write("#{ENV["FOLDER"]}/collated.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment