I am trying to export data from my models to an Excel spreadsheet. I saw 3 ways
My choice is to simply manually create the CSV file. How:
File.new("data.csv", "w+") do |f| @my_data.each do |data| f << [data.title, data.body, ...].join(", ") + "\n" end end
A CSV file can be opened using excel or any other soft table.
I am using writeexcel in my latest Rails project. Fast and easy way to export excel files directly - no CSV !
To use it directly in your views, you need to register writeexcel as a template handler - this is the exception that my gist . Then create a new template, for example export.xls.writeexcel , paste in your code, and you will go well.
export.xls.writeexcel
You can connect my own gem here, but you can take a look at https://github.com/randym/acts_as_xlsx
This gives you a little more than writeexcel or a spreadsheet in terms of localization, graphs, tables, and formatting from the axlsx gem.
It is also integrated with active coverage ranges and method objectives.
Blogpost with detailed usage examples: http://axlsx.blogspot.com/
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
On Github: https://github.com/randym/axlsx
On Rubygems: https://rubygems.org/gems/axlsx
On Rubytookbox: https://www.ruby-toolbox.com/projects/axlsx
This is mainly due to 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 next on your model
class Post < ActiveRecord::Base acts_as_xlsx
The two blog posts above give a pretty clear walk.
Source: https://habr.com/ru/post/893109/More articles:Get the sum of powers of 2 for a given number + C # - c #Is there a way to defer WPF DataBinding (improve rendering)? - data-bindingHow to call library c from assembly code on Linux? - cLog4Net: Debug vs Info? - loggingRegex to find a float, maybe a REALLY simple question - javaJava floating-point number in String - javaWhere is my database saved when I create it in MySQL? - databaseJava String Manipulation: extracting an integer and float from a string based on a pattern - javaErrors Using ThashSet from DeHL Collection Library - delphiCheck the exit status of the last command in ipython - pythonAll Articles