Search code examples
rubyxlsxroo

How do I open a new XLSX file in roo?


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?


Solution

  • 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.