Rails 4,axlsx和acts_as_xlsx:如何使每个列自己的工作表?

gem

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表格中,每个属性都是一个列。 不过,为了我的目的,我希望每一篇文章都是自己的文章。 我对这两个gem都很陌生,在进行这种调整时遇到了麻烦。

有没有人已经完成了这个可以分享他们做了什么?

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

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 }