如何将数据从模型导出到rubyonrails上的excel文件

时间:2021-11-20 01:58:02

I'm trying to export data from my models to an excel spreadsheet. I have seen 3 ways

我正在尝试将数据从我的模型导出到Excel电子表格。我见过3种方法

  1. Using the spreadsheet gem which I didn't understand how to use it, the examples I saw was writing to a local file but I'm looking to generate a file every time user clicks on a link.
  2. 使用我不了解如何使用它的电子表格gem,我看到的示例是写入本地文件,但我希望每次用户点击链接时生成一个文件。
  3. Creating a method called export, and running the query there, then making a export.xls file in my view, and that file creating the table I want to be exported to the excel file, but this approach don't allow me to create multiple sheets.
  4. 创建一个名为export的方法,然后在那里运行查询,然后在我的视图中创建一个export.xls文件,然后创建该表的文件我想导出到excel文件,但是这种方法不允许我创建多个床单。
  5. Followed this tutorial, http://oldwiki.rubyonrails.org/rails/pages/HowToExportToExcel, but here doesn't show how to put the link in the view, looks to me that I'm missing something in the routes, I can give github so you can take a look at my code if needed.
  6. 按照本教程http://oldwiki.rubyonrails.org/rails/pages/HowToExportToExcel,但这里没有说明如何将链接放在视图中,看着我在路线中遗漏了一些东西,我可以给github所以你可以根据需要查看我的代码。

3 个解决方案

#1


7  

My choice is to just manualy generate CSV file. Like:

我的选择是只是手动生成CSV文件。喜欢:

File.new("data.csv", "w+") do |f|
  @my_data.each do |data|
    f << [data.title, data.body, ...].join(", ") + "\n"
  end
end

CSV file can be opened with excel or any other spreadsheet soft.

可以使用excel或任何其他电子表格软件打开CSV文件。

#2


1  

I'm using writeexcel in my most recent Rails project. A fast and simple to use way to export excel files directly - no CSV!

我在最近的Rails项目中使用了writeexcel。一种快速且简单易用的直接导出Excel文件的方法 - 无需CSV!

To use it directly in your views you have to register writeexcel as a template handler - this is excalty what my gist does. Then create a new template like export.xls.writeexcel, insert your code and you're good to go.

要在视图中直接使用它,你必须将writeexcel注册为模板处理程序 - 这是我的要点所做的事情。然后创建一个新的模板,如export.xls.writeexcel,插入你的代码,你就可以了。

#3


1  

Plugging my own gem here, but you might have a look at https://github.com/randym/acts_as_xlsx

在这里插入我自己的宝石,但你可以查看https://github.com/randym/acts_as_xlsx

It gives you a bit more than writeexcel or spreadsheet in terms of localization, graphs, tables and formatting from the axlsx gem.

它在axlsx gem的本地化,图形,表格和格式方面为您提供了比writeexcel或电子表格更多的功能。

It also integrated with active record scoping and method chains.

它还与活动记录范围和方法链集成。

Blogpost with detailed usage examples: http://axlsx.blogspot.com/

Blogpost详细用法示例:http://axlsx.blogspot.com/

http://axlsx.blogspot.jp/2011/12/using-actsasxlsx-to-generate-excel-data.html

http://axlsx.blogspot.jp/2011/12/using-actsasxlsx-to-generate-excel-data.html

http://axlsx.blogspot.jp/2011/12/axlsx-making-excel-reports-with-ruby-on.html

http://axlsx.blogspot.jp/2011/12/axlsx-making-excel-reports-with-ruby-on.html

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

Basically it involves setting up a responder in your controller

基本上它涉及在控制器中设置响应器

    format.xlsx {
      xlsx_package = Post.to_xlsx
      begin
        temp = Tempfile.new("posts.xlsx")
        xlsx_package.serialize temp.path
        send_file temp.path, :filename => "posts.xlsx", :type => "application/xlsx"
      ensure
        temp.close
        temp.unlink
      end
    }  

and the following on your model

以及您的模型上的以下内容

     class Post < ActiveRecord::Base
       acts_as_xlsx

The two blog posts above give a fairly clear walk-through.

上面的两篇博客文章给出了相当明确的演练。

#1


7  

My choice is to just manualy generate CSV file. Like:

我的选择是只是手动生成CSV文件。喜欢:

File.new("data.csv", "w+") do |f|
  @my_data.each do |data|
    f << [data.title, data.body, ...].join(", ") + "\n"
  end
end

CSV file can be opened with excel or any other spreadsheet soft.

可以使用excel或任何其他电子表格软件打开CSV文件。

#2


1  

I'm using writeexcel in my most recent Rails project. A fast and simple to use way to export excel files directly - no CSV!

我在最近的Rails项目中使用了writeexcel。一种快速且简单易用的直接导出Excel文件的方法 - 无需CSV!

To use it directly in your views you have to register writeexcel as a template handler - this is excalty what my gist does. Then create a new template like export.xls.writeexcel, insert your code and you're good to go.

要在视图中直接使用它,你必须将writeexcel注册为模板处理程序 - 这是我的要点所做的事情。然后创建一个新的模板,如export.xls.writeexcel,插入你的代码,你就可以了。

#3


1  

Plugging my own gem here, but you might have a look at https://github.com/randym/acts_as_xlsx

在这里插入我自己的宝石,但你可以查看https://github.com/randym/acts_as_xlsx

It gives you a bit more than writeexcel or spreadsheet in terms of localization, graphs, tables and formatting from the axlsx gem.

它在axlsx gem的本地化,图形,表格和格式方面为您提供了比writeexcel或电子表格更多的功能。

It also integrated with active record scoping and method chains.

它还与活动记录范围和方法链集成。

Blogpost with detailed usage examples: http://axlsx.blogspot.com/

Blogpost详细用法示例:http://axlsx.blogspot.com/

http://axlsx.blogspot.jp/2011/12/using-actsasxlsx-to-generate-excel-data.html

http://axlsx.blogspot.jp/2011/12/using-actsasxlsx-to-generate-excel-data.html

http://axlsx.blogspot.jp/2011/12/axlsx-making-excel-reports-with-ruby-on.html

http://axlsx.blogspot.jp/2011/12/axlsx-making-excel-reports-with-ruby-on.html

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

Basically it involves setting up a responder in your controller

基本上它涉及在控制器中设置响应器

    format.xlsx {
      xlsx_package = Post.to_xlsx
      begin
        temp = Tempfile.new("posts.xlsx")
        xlsx_package.serialize temp.path
        send_file temp.path, :filename => "posts.xlsx", :type => "application/xlsx"
      ensure
        temp.close
        temp.unlink
      end
    }  

and the following on your model

以及您的模型上的以下内容

     class Post < ActiveRecord::Base
       acts_as_xlsx

The two blog posts above give a fairly clear walk-through.

上面的两篇博客文章给出了相当明确的演练。