Achim Achim - 6 months ago 29
Ruby Question

Reading and writing Excel files using Ruby on a server without Excel installed

I need to read and write (-> transform) Excel files on a Linux server, which of course does not have Excel installed. For Python there exists Is there something similar for Ruby? Processing of the latest Office format is probably not required. Just old xls files should be enough.


I agree with Gonzih, and I use roo fairly regularly. It allows me to read, write, and write using a template file. The project is fairly well documented on their site.

I always use something like:

input =
output =
input.default_sheet = input.sheets[sheet]
start.upto(input.last_row) do |row|
  output << input.row(row)

p output
=> a nested array representing the spreadsheat.

p output[0]
=> [row1_column_a, row1_column_b...]

to read a spreadsheet. note that the roo gem requires you to use instead of if your file is a .xlsx.

to write you can:

book =
write_sheet = book.create_worksheet
row_num = 0
input.each do |row|
  write_sheet.row(row_num).replace row
  row_num +=1
book.write "/path/to/save/to.xls"

where input is an array structured just like output was