How to generate an Excel file with Rails?

Yuval Karmi picture Yuval Karmi · Aug 14, 2011 · Viewed 32.8k times · Source

I was searching Ruby toolbox for a popular, well-supported tool to generate XSLX (Excel 2007 and on) document, but I didn't manage to find anything. I also spent a good amount of time searching on Google, but most of the answers I found seem outdated.

I'll need to include inline images in the document I generate.

I'm working with Ruby 1.9.2 and Rails 3.

Any suggestions?

Thank you very much!

Answer

randym picture randym · Apr 18, 2012

Bit late to the game, but there you go. You should use the axlsx gem

On Github: https://github.com/randym/axlsx

On Rubygems: https://rubygems.org/gems/axlsx

On Rubytookbox: https://www.ruby-toolbox.com/projects/axlsx

From the README

p = Axlsx::Package.new
p.workbook do |wb|
  wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
    img = File.expand_path('../image1.jpeg', __FILE__)
    sheet.add_image(:image_src => img, :noSelect => true, :noMove => true,  :hyperlink=>"http://axlsx.blogspot.com") do |image|
      image.width=720
      image.height=666
      image.hyperlink.tooltip = "Labeled Link"
      image.start_at 2, 2
    end
  end
end