Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate an Excel file with Rails?

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!

like image 674
Yuval Karmi Avatar asked Aug 14 '11 10:08

Yuval Karmi


People also ask

How do I create an Excel spreadsheet from a website?

Open or select the workbook that contains the data that you want to publish. To save only part of the data on a sheet as a webpage, select the data that you want to save. On the File menu, click Save as Web Page.


1 Answers

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
like image 90
randym Avatar answered Sep 27 '22 18:09

randym