如何用Rails生成Excel文件?

发布于 2024-11-29 06:21:03 字数 215 浏览 1 评论 0原文

我在 Ruby 工具箱中搜索流行的、支持良好的工具来生成 XSLX(Excel 2007 及以上)文档,但我没有找到任何东西。 我也花了很多时间在谷歌上搜索,但我找到的大多数答案似乎都已经过时了。

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

我正在使用 Ruby 1.9.2 和 Rails 3。

有什么建议吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后彩虹 2024-12-06 06:21:03

比赛有点晚了,但就这样吧。
你应该使用 Github 上的 axlsx gem


https://github.com/randym/axlsx

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

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

来自自述文件

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

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
早乙女 2024-12-06 06:21:03

我使用了电子表格 gem。这是我用来将其写入文件并发送的一些代码。

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'

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

我没有看到内联图像的需要,但我会留下这个,因为它可能对其他人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文