1

宝石

https://github.com/randym/axlsx

https://github.com/randym/acts_as_xlsx

教程

http://axlsx.blog.randym.net/2011/12/axlsx-making-excel-reports-with-ruby-on.html

在本教程中,将 Post 模型添加到 Excel 工作表中,每个属性都是一列。但是,出于我的目的,我希望每个帖子都成为自己的工作表。我对这两种宝石都很陌生,并且在进行调整时遇到了麻烦。

有没有人已经完成了可以分享他们所做的事情?

4

1 回答 1

0

参考示例,您可以为每列创建一个工作簿,并将每个工作簿的第一个工作表复制到一个新工作簿中,其中包含模型的所有工作表

format.xlsx {
  p = Axlsx::Package.new
  wb = p.workbook

  col_1 = Post.to_xlsx({:columns => [:id]})
            :name => "Posts1"

  wb.worksheets[0] = col_1.workbook.worksheets.first

  col_2 = Post.to_xlsx({:columns => [:content]})
            :name => "Posts2"

  wb.worksheets[1] = col_2.workbook.worksheets.first

  begin
    temp = Tempfile.new("posts.xlsx")
    p.serialize temp.path
    send_file temp.path, :filename => "posts.xlsx", :type => "application/xlsx"
  ensure
    temp.close
    temp.unlink
  end
}  
于 2015-05-03T13:35:35.920 回答