Search code examples
ruby-on-railsrubyxl

Downloading workbook without saving it to disk rubyxl in Rails


Rails 4.2
RubyXL

I am using the RubyXL to create a workbook. I can write it disk with:

workbook.write("path/to/desired/Excel/file.xlsx")

and then download it with something like this:

send_file(workbook.write("path/to/desired/Excel/file.xlsx"), options = {:filename => 'myworkbook.xlsx', :disposition => 'attachment'})

Any suggestions on how to download it without having to save it to the server first?


Solution

  • You may convert workbook directly to a string:

    def get_worksheet_as_string
      workbook = RubyXL::Workbook.new
      # Fill workbook here or leave as is to download empty
      send_data workbook.stream.string, filename: "myworkbook.xlsx",
                                        disposition: 'attachment'
    end