Saturday, February 4, 2012

Export csv at a particular pre-defined folder in rails application

Export excel from Rails application - User gets a link on view and when user clicks on that link user is not asked about the location for saving the csv, csv is exported and saved at pre-defined location. In this case, you cannot create excel view.
Saving the csv at a particular pre-defined folder
Code snippets:

1. Add action on 'eqrawdatas' controller: 
def export_list
  require 'csv'
  symboldetails = Masterdata.find(:all)
  config = read_config_exp  

    outfile = File.open(File.join(config['event_export_path'],"export_data.csv"), "w")
      CSV::Writer.generate(outfile) do |csv|
      csv << Array["Uniq Symbol", "Symbol", "Data Date" ]
      symboldetails.each do |symboldetail|
        arr = []
    arr = Array[symboldetail.uniqsymbol, symboldetail.symbol, symboldetail.data_date.strftime("%b %d, %Y")  ]
        csv << arr                                   
      end
    end
    outfile.close 
  return redirect_to :back
end

2. In the same controller, add a private method
  private
  #read the config file

  def read_config_exp
    config_file = File.open("#{RAILS_ROOT}/misc/export_event.yml")
    erb = ERB.new(config_file.read)
    all_configs = YAML.load(erb.result(binding))
    dir = all_configs["All"]["event_export_path"]
    Dir.mkdir(dir) unless File.exists?(dir)
    all_configs['All']
  end

3. Create export_event.yml file in misc folder inside rails application
In rails application misc folder, create export_event.yml file. In this file it will export the csv data in C:\exported_csv folder:
All:
  event_export_path: C:\exported_csv

4. Add link on view:
<%= link_to("Export EQ Date Data List", :controller => :eqrawdatas, :action => :export_list) %>
 

Prompting and letting user save csv in a desired folder

Export excel from Rails application - User gets a link on view and when user clicks on that link a prompt comes asking user to save the exported excel on clients computer at desired location. In this case, you can create excel view and perform view formatting as we do with html page.
Prompting and letting user save csv in a desired folder:
Code snippets:

1. Add in routes:
map.fumasterdatalist 'chart/fumasterexcel', :controller => "chart", :action => "fumaster_excel"

2. Add link on view:
   <%= link_to "Export FU List as Excel", {:controller=> "chart", :action=> "fumaster_excel"}  %>

3. Add action on controller
  def fumaster_excel
      @oprawdatas= Expdatedata.find(:all, :order=> "exp_date asc")
      headers['Content-Type'] = "application/vnd.ms-excel"
      headers['Content-Disposition'] = 'attachment; filename="fuexpreport.xls"'
      headers['Cache-Control'] = ''
      render(:layout=>false)
    end

4. Create a view page which will be exported in excel:

<table "border=1px" cellpadding="2" style="margin: 20px 40px">
  <tr>
   <th>Sl. No.</th>
   <th>ID</th>
   <th>Instrument</th>
    <th>Symbol</th> 
    <th>Created_At&nbsp;&nbsp;&nbsp;</th>
  </tr>

<% i = 0 %>
<% @oprawdatas.each do |import| %>
  <tr>
  <td><%=i = i + 1 %></td>
  <td><%=h import.id %></td>
    <td><%=h import.instrument %></td>
    <td><%=h import.symbol %></td> 
    <td><%=h import.created_at.strftime("%b %d, %Y") %></td>
    <td><%= link_to 'Destroy', {:controller=> "oprawdatas", :action=> "destroy", :id=> import.id}, :confirm => 'Are you sure you want to delete the record?' %></td>
  </tr>
<% end %>
</table>



"src/rbae.c:13:27: fatal error: Carbon/Carbon.h: No such file or directory" while installing rb-appscript (0.6.1) on ubuntu

While running bundle install, got following error:
Installing rb-appscript (0.6.1) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/ritesh/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb
extconf.rb:44: Use RbConfig instead of obsolete and deprecated Config.
create /home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1/src/osx_ruby.h ...
create /home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1/src/osx_intern.h ...
creating Makefile

make
Makefile:226: warning: overriding commands for target `/home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1/lib'
Makefile:224: warning: ignoring old commands for target `/home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1/lib'
compiling src/rbae.c
src/rbae.c:13:27: fatal error: Carbon/Carbon.h: No such file or directory
compilation terminated.
make: *** [rbae.o] Error 1


Gem files will remain installed in /home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1 for inspection.
Results logged to /home/ritesh/.rvm/gems/ruby-1.9.3-p0@koodrails/gems/rb-appscript-0.6.1/./gem_make.out
An error occured while installing rb-appscript (0.6.1), and Bundler cannot continue.
Make sure that `gem install rb-appscript -v '0.6.1'` succeeds before bundling.




Yet looking for solution to fix the issue, if someone can help.