Reputation: 26713
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!
Upvotes: 13
Views: 39806
Reputation: 4295
I've used the spreadsheet gem. Here is some code that I've used to write it to a file and send.
spreadsheet_name = @role.title
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => spreadsheet_name
@people.each_with_index { |person, i|
sheet1.row(i).replace [person.first_name, person.last_name, person.email, person.title, person.organization, person.phone, person.street, person.street2, person.city, person.state, person.zip, person.country]
}
export_file_path = [Rails.root, "public", "uploads", "exports", "#{ @team.sort_name }_#{ DateTime.now.to_s }.xls"].join("/")
book.write export_file_path
send_file export_file_path, :content_type => "application/vnd.ms-excel", :disposition => 'inline'
There is probably a way to do this in memory and not write a file.. but I didn't take the time to look into it.
Upvotes: 10
Reputation: 2460
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
Upvotes: 40