Search code examples
ruby-on-railsrubyruby-on-rails-2

Can't export more than 5000 rows to Excel?


I wrote an application to export information to an Excel file (XLS), but it isn't exporting all my rows (11,444 rows) and is only exporting 5,630 rows.

Here is my table:

##### I HAVE MORE THAN 5000 rows
|policies|
   |id| |num_policy| |money| 
    1     12345       1000
    2     45151       2000
    3     15488       1300
    ...   ...         ...     

Here is my controller: (proyect/app/controller/policy_controller.rb)

class PolicyController < ApplicationController

 def exportation

  @policies = Policy.paginate(:page => params[:page], :per_page => 10)
  @results= Policy.find(:all)

  respond_to do |format|
    format.html
    format.xls { send_data render_to_string(:partial=>"report"), :filename => "Report.xls" }
  end

Here is my view: (proyect/app/views/exportation.html.erb)

<% @policies.each do |policy| %>
  <%= policy.id %> 
  <%= policy.num_policy %> 
  <%= policy.money %> 
<% end %>

<%= link_to "Export Excel",{:controller=>"policy",:action=>"exportation", :format=>"xls",:page => params[:page] }  %>

Here is my view: (proyect/app/views/_report.erb) but is not exporting more than 500

<% @results.find_each(:batch_size => 8000) do |policy| %>
  <%= policy.id %> 
  <%= policy.num_policy %> 
  <%= policy.money %> 
<% end %>

Here is my log:

Rendered policy/_report (64280.5ms)
Sending data Report.xls
Completed in 64394ms (View: 0, DB: 0) | 200 OK [http://0.0.0.0/policy/exportation?format=xls]

I tried:

<% @results.each do |policy| %>
  <%= policy.id %> 
  <%= policy.num_policy %> 
  <%= policy.money %> 
<% end %>

And also removed :page => params[:page] and it didn't work.

 <%= link_to "Export Excel",{:controller=>"policy",:action=>"exportation", :format=>"xls"}  %>

And also added a count to make sure that it has 11,444 rows.

class PolicyController < ApplicationController
  def exportation
    @policies = Policy.count
    @results  = Policy.count
  end
end

SELECT count(*) AS count_all FROM `policies`
###   And I got 11 444 rows on both.

Is not my query because I checked it on MySQL.

I spent one week searching information about this problem.

Seems that I need to add something else.

Can someone help me with this?


Solution

  • Well after doing my last testing i found the answer and was very easy.

    I was using LINUX UBUNTU 64 bit and it uses OPEN OFFICE (libre calc) and this program is only showing 5000 rows.

    The solution is:

                     *** Open the file using Microsoft Excel on Windows. ***
    

    Thank you guys for helping.