2016-10-10 9 views
3

Rubyの新機能です。ビューテーブルをExcel形式でエクスポートしたいのですが、何度も試してみましたが、ビューテーブルをエクスポートできません。 私のデータベースにはビュー(Employee_Information)があり、view.html.erbページにはすべてのデータが表示されますが、Employee_Informationビューの具体的なモデルはありません。私はこのチュートリアル "http://railscasts.com/episodes/362-exporting-csv-and-excel"に従っていますが、私はこのチュートリアルのデータをモデルの助けを借りてビューテーブルからエクセルにエクスポートしています。ビューテーブルをExcel形式に変換する方法

**excel_file.html.erb** 
`<table border=1> 
    <tr> 
    <th>Employee ID</th> 
    <th>Name</th> 
    <th>Gender</th> 
    <th>Date of birth</th> 
    <th>Email</th> 
    <th>Marital status</th> 
    <th>Father Name</th> 
    <th>Spouse Name</th> 
    <th>Contact</th> 
    <th>Employee Address</th> 
    <th>Account Number</th> 
    <th>IFSC Code</th> 
    <th>PAN Number</th> 
    <th>Client Name</th> 
    <th>Designation</th> 
    <th>Employee Type</th> 
    <th>Status</th> 
    <th>Joining date</th> 
    <th>End date</th> 
    <th>Offer CTC</th> 
    <th>Client Address</th>  
    </tr> 
    <% @employees.each do |emp| %> 
    <tr> 
     <th><%= emp['employee_id'] %></th> 
     <th><%= emp['full_name'] %></th> 
     <th><%= emp['gender'] %></th> 
     <th><%= emp['dob_date'] %></th> 
     <th><%= emp['email'] %></th> 
     <th><%= emp['married_status'] %></th> 
     <th> <%= emp['father_name'] %></th> 
     <th><%= emp['spouse_name'] %></th> 
     <th><%= emp['contact_phone'] %></th> 
     <th><%= emp['candidate_address2'] %></th> 
     <th><%= emp['bank_ac'] %></th> 
     <th><%= emp['bank_ifsc'] %></th> 
     <th><%= emp['pan_number'] %></th> 
     <th><%= emp['company_name'] %></th> 
     <th><%= emp['designation'] %></th> 
     <th> 
      <% if emp['employee_type'] == 0 %> 
       Internal Employee 
      <% elsif emp['employee_type'] == 1 || emp['employee_type'] == 2 || emp['employee_type'] == 3 %> 
       Contract Consultant Employee 
      <% elsif emp['employee_type'] == 4 %> 
       Permanent Consultant Employee 
      <% elseif emp['employee_type'] == 5 %> 
       Past Employee 
      <% end %> 
     </th> 
     <th> 
      <% if emp['status'] == 0 %> 
       Pending 
      <% elsif emp['status'] == 1 %> 
       Approved 
      <% elsif emp['status'] == 2 %> 
       Cancelled 
      <% elsif emp['status'] == 3 %> 
       Accepted 
      <% elsif emp['status'] == 4 %> 
       Rejected 
      <% elsif emp['status'] == 5 %> 
       Onboarded 
      <% elsif emp['status'] == 6 %> 
       Offboarded 
      <% end %> 
     </th> 
     <th><%= emp['joining_date'] %></th> 
     <th><%= emp['work_end_date'] %></th> 
     <th><%= emp['ctc'] %></th> 
     <th><%= emp['client_address2']%></th> 
    </tr> 
    <% end %> 
</table> 
<br><br>` 

**hr_controller.rb**`def excel_file 
     @employees = MysqlConnection.connection.select_all("SELECT * FROM   Employee_Information where employee_type IN(0)") 
     #@employees = MysqlConnection.connection.select_all("SELECT * FROM Employee_Information where employee_type IN(1,2,3)") 
     respond_to do |format| 
      format.html 
      format.csv { send_data @employees.to_csv } 
     end 
    end` 
**application.rb** 



    require File.expand_path('../boot', __FILE__) 
    # add HR role 
    require "csv" 

私は

module EmployeeInformation  
     def self.to_csv(options = {}) 
     CSV.generate(options) do |csv| 
      csv << column_names 
      all.each do |product| 
       csv << product.attributes.values_at(*column_names) 
      end 
     end 
    end end 
+0

なぜモデルを使用しますか –

答えて

0

libフォルダにファイルモジュール "をemployee_information.rb" を作成しましたこれは、ビューテーブルでなければなりませんか?これを何らかの形にしない限り、javascriptを使用してDOMを解析したり、DOMのすべての要素にアクセスしたりせずに値を取得し、解析された情報からコントローラに要求します。 DOM。これを何らかの形で提出することができれば、この情報をエクセルにエクスポートする方がはるかに簡単です。いずれにしても、ある種のモデルでこれを行う別の方法は実際にはありません。

関連する問題