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

Updated . Posted . Visible to the public.

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
Last edit
Keywords
table
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-06-01 13:51)