ZachyBear ZachyBear - 6 months ago 19
SQL Question

Exporting data from Rails into XML and Excel

I use RubyOnRails and I want to get the data from within my database into an XML or Excel format.

How can I do it?

Answer

Exporting to XML is simple, you can call to_xml method on your items and render the result or use render :xml

http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

So for example, in your controller:

respond_to do |format|
    format.html
    format.xml { render xml: @items }
end

When it comes to Excel, there is a railscast on exporting to CSV (which can be easily imported to Excel) and XLS.

http://railscasts.com/episodes/362-exporting-csv-and-excel

Comments