Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export to excel,pdf and doc using Ruby on Rails

Could you please help me in exporting files to excel,pdf and .doc format through rails applications??

like image 911
Sumeru Suresh Avatar asked Mar 29 '10 11:03

Sumeru Suresh


1 Answers

For Excel

  1. I've used FasterCSV and this post to generate csv files that open in excel very well.
  2. You can also create an HTML table and use iqy files to load excel. To do this you can create some new aliases and then handle the new .iqy and .excel formats.

In config/initializers/mime_types.rb

Mime::Type.register_alias "text/html", :excel
Mime::Type.register "text/x-ms-iqy", :iqy

Then in your controller handle the .iqy and .excel formats

respond_to do |format|
  format.html
  format.excel
  format.iqy {
    text = "WEB\n1\n%s" % URL_FOR_DATA_VIA_GET, :format => :excel)
    render :text => text
  }
end

Your template for the excel format should just be an HTML table with the data in it. The iqy format just renders text, now action template needed. You may want an application.excel.erb file that doesn't have all your navigation etc, so you get a nice clean html table. Also see this KB article for more iqy information.

For PDF

  1. See this other SO question, the suggested plug-in looks good, but I haven't used it.
  2. I've played with, but never actually used in production, prawn. It seemed good, just haven't used it.

For Doc

I would suggest that you just stick with simple text or RTF. I'm not aware of any plug-ins for this, but there is probably something.

Another approach

I've used JasperReports on pure Java projects with great success. It would be possible to use the JasperServer product, JRuby, your own application wrapper, or the ruby-java bridge to generate the outputs with Jasper. See this post. Once you go part Java, you also get the wonderful JExcelApi.

like image 65
danivovich Avatar answered Nov 17 '22 06:11

danivovich