Dump a two-dimensional array to an Excel .xls spreadsheet

Copy the attached Ruby code to config/initializers, or paste it into your IRB console. You can now dump any two-dimensional array to an Excel .xls spreadsheet with a single method call:

table = [['user', 'email', 'hours']]
User.all.each do |user|
  table << [user.name, user.email, user.hours]
end

table.dump_to_excel('users.xls')

The first row in the array will be dumped as the table head in bold type.

You need the spreadsheet gem in your Gemfile.

Note that there is a limit of 65535 rows per XLS worksheet.

Henning Koch