0

Is there any way or example to export xls on rails 2.3.5 without using gems and something complicated?

I'm trying to export to xls with conditions by ejectuve_id that i selected i have this code

 *********It s in my controller********  
      @search_ejecutive = params[:search_ejecutive].to_i
      @search_status = params[:status_id].to_i
      @list_ejecutives_comision = Ejecutive.find(:all)
      ejecutive_ids = []

      obj_user.ejecutives.each do |ejecutive|
        @list_ejecutives_comision << Ejecutive.find(ejecutive.id)
      end

      @list_policies_search = Policy.deleted_is(0)

      if params[:search_ejecutive].to_i!=0
        @list_policies_search = @list_policies_search.ejecutive_id_is(@search_ejecutive)
      end

      ejecutive_ids = []
      obj_user.ejecutives.each do |ejecutive|
         ejecutive_ids << ejecutive.id
      end

      if !ejecutive_ids.blank?
          @list_policies_search = @list_policies_search.ejecutive_id_in(ejecutive_ids)
      end 

     @list_policies_search = @list_policies_search.deleted_is(0)
     @list_policies = @list_policies_search.paginate(:page => params[:page], :per_page => 10)

And here i'm showing links that i tried

    <% form_remote_tag :url=>{:action=>"generate_print_ejecutive_comercial"},:before=>"load_close('loading_search')",:success=>"load_off('loading_search')" do -%>


    <label>Ejecutivo:</label>
    <%= select_tag 'search_ejecutive',"<option value=\"\">Seleccione</option>"+options_for_select(@list_ejecutives_comision.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]})%>

   <input name="Buscar" value="Buscar" type="submit" /><span id="loading_search"></span>
<% end %>
  <%= link_to("xls","http://localhost:3000/policy_management/policy/generate_print_ejecutive/generate_print_ejecutive.xls")%>

  <%= link_to "xls", :controller=>"policy_management/policy",:action=>"generate_print_ejecutive",:format=>"xls" ,:search => params[:search_ejecutive],:page => params[:page]    %>

  <%= link_to 'Imprimir', :controller=>"policy_management/policy",:action=>"generate_print_ejecutive", :format=>"pdf" %>
   <div id="table">
<%= render :partial=>"table2" %>

My :partial table2 is the result of @list_policies My teacher doesn't want doesn't want to reveal me the secret ,told me that the trick is

@list_policies

I tried to export manually and i tried this

@list_policies.find(:all,:conditions=>"ejecutive id = 1")

Somebody can help me with this, my teache told me that i need to export :search_ejecutive params

I will appreciate all help

4

1 回答 1

0

There is a way by which newest format of xls can be represented by an xml, you can read more about it here.

So your controller can answer .xls requests, and you can then have a view generate_print_executive.xls.erb like this one:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Sheet1">
    <Table>
      <% @list_policies.each do |policy| %>
        <Row>
          <Cell>
            <Data ss:Type="String"><%= policy.column_data %></Data>
          </Cell>
        </Row>
      <% end %>
    </Table>
  </Worksheet>
</Workbook>

Notice this is just a dummy example on how to generate a .xls without using any gem, I wasn't able to understand what you need on your post, I'm mostly answering the title of the post :P

于 2013-09-13T23:35:24.907 回答