如何使用Rails生成Excel文件?

时间:2023-02-05 22:46:08

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.

我在Ruby工具箱中搜索一个流行的,支持良好的工具来生成XSLX(Excel 2007及更高版本)文档,但我找不到任何东西。我也花了很多时间在Google上搜索,但我发现的大多数答案似乎已经过时了。

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

我需要在我生成的文档中包含内嵌图像。

I'm working with Ruby 1.9.2 and Rails 3.

我正在使用Ruby 1.9.2和Rails 3。

Any suggestions?

有什么建议么?

Thank you very much!

非常感谢你!

3 个解决方案

#1


34  

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

比赛迟到了,但你去了。你应该使用axlsx gem

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

在Github上:https://github.com/randym/axlsx

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

在Rubygems上:https://rubygems.org/gems/axlsx

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

在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

#2


8  

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.

可能有一种方法可以在内存中执行此操作而不是写入文件..但我没有花时间去研究它。

I didn't see the inline images need, but I'll leave this because it might help someone else.

#3


1  

You can try this gem

你可以尝试这个宝石

https://github.com/harvesthq/simple_xlsx_writer

https://github.com/harvesthq/simple_xlsx_writer

And

https://github.com/cxn03651/writeexcel/

https://github.com/cxn03651/writeexcel/

#1


34  

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

比赛迟到了,但你去了。你应该使用axlsx gem

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

在Github上:https://github.com/randym/axlsx

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

在Rubygems上:https://rubygems.org/gems/axlsx

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

在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

#2


8  

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.

可能有一种方法可以在内存中执行此操作而不是写入文件..但我没有花时间去研究它。

I didn't see the inline images need, but I'll leave this because it might help someone else.

#3


1  

You can try this gem

你可以尝试这个宝石

https://github.com/harvesthq/simple_xlsx_writer

https://github.com/harvesthq/simple_xlsx_writer

And

https://github.com/cxn03651/writeexcel/

https://github.com/cxn03651/writeexcel/