-1

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?

luchaninov
  • 6,792
  • 6
  • 60
  • 75
ZachyBear
  • 297
  • 3
  • 15
  • What kind of database are you using? SQLite? If so, you may find this post useful: http://stackoverflow.com/questions/6076984/how-to-export-the-results-of-my-query-to-csv-file-sqlite. Excel can easily handle CSV files. – Nicholas Flees Apr 14 '14 at 17:38
  • Sorry. Didn't notice that you tagged the post with mysql. This answer should help: http://stackoverflow.com/a/356605/868691 – Nicholas Flees Apr 14 '14 at 17:44

1 Answers1

2

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

Jakub K
  • 1,713
  • 1
  • 13
  • 21
  • Is there a way to get it to xml file in the rails c? – ZachyBear Apr 14 '14 at 22:18
  • Sure, you can use the `to_xml` method on your collection and save the result into a file. For help with writing to files see http://stackoverflow.com/questions/2777802/how-to-write-to-file-in-ruby – Jakub K Apr 14 '14 at 22:24
  • after trying: orders = Order.all_to_xml() I got this error NoMethodError: undefined method `to_xml' for # – ZachyBear Apr 15 '14 at 14:43
  • Try `orders = Order.all.to_xml` – Jakub K Apr 15 '14 at 15:21
  • do you know where the default location the xml file would be saved to? – ZachyBear Apr 16 '14 at 17:09
  • You have to save the file yourself, such as `File.open('filename', 'w') { |f| f.write(Order.all.to_xml) }`; the file `filename` should be saved in the root folder of your app. – Jakub K Apr 16 '14 at 17:24
  • and after that I got a 63112976 returned in rails c, know what this means? – ZachyBear Apr 16 '14 at 19:20
  • Looks like the number of characters written – Jakub K Apr 16 '14 at 20:08
  • awsome thanks, found the files in vim and now I just need to export them to excel and I'm done! I am so close thanks again for all your help Jacob – ZachyBear Apr 16 '14 at 20:23
  • is there a way to save the to_xml file to desktop or any other directory on my computer? I am having a hard time getting those xml files out of vimT.T – ZachyBear Apr 16 '14 at 21:46