0

This code:

newBook = Roo::Excelx.new('./test.xlsx')

Gives me this error:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/roo-1.13.2/lib/roo/excelx.rb:85:in `block in initialize': file ./test.xlsx does not exist (IOError)

Why? How do I make a new XLSX file with Ruby's roo gem?

Username
  • 3,463
  • 11
  • 68
  • 111
  • 3
    roo only allows for reading Excel files. You can write google spreadsheets with roo-google or you can try axlsx gem (which is my favorite) – engineersmnky Apr 23 '15 at 01:18
  • @engineersmnky Hm, `axlsx` seems like it's for Rails. Am I wrong? – Username Apr 23 '15 at 02:17
  • 1
    No axlsx can be used in pure ruby. I use it all the time to run exports and other excel reports. I will post a quick answer to show you. – engineersmnky Apr 23 '15 at 12:56

1 Answers1

2

Roo is meant for reading Excel files only. I would recommend the axlsx gem.

It can be used in pure ruby as follows

require 'axslx'
package = Axlsx::Package.new
workbook = package.workbook
workbook.add_worksheet(name: 'Some Sheet Name') do |sheet|
  sheet.add_row ["Header 1", "Header 2", "Header 3"]
  sheet.add_row ["Data 1", "Data 2", "Data 3"]
end
package.serialize('./test.xlsx')

This will create a spreadsheet that looks like

 --------------------------------
| Header 1 | Header 2 | Header 3 |
 --------------------------------
| Data 1   | Data 2   | Data 3   |

axlsx offers pretty much everything you can do in excel including styling and conditional styling. Hope this helps you out.

Pedro Gabriel Lima
  • 1,122
  • 1
  • 9
  • 24
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • Great! When adding rows, is there a way to specify which cell I want the data to be in? Example: I only want data to go in the "Header 2" column. How would I do that? – Username Apr 23 '15 at 16:34
  • 1
    @Username yes it would be `sheet.add_row [nil,"Data"]` `add_row` will create a new row +1 from the last. The Array passed in will act as the row so `[nil,"Data"]` will be a blank cell then "Data". – engineersmnky Apr 23 '15 at 18:14