【问题标题】:how to convert my view table into excel format如何将我的视图表转换为 excel 格式
【发布时间】:2016-10-10 06:52:54
【问题描述】:

我是 ruby​​ on rails 的新手,我想以 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"

我在 lib 文件夹中创建了一个文件模块“employee_information.rb”

  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

【问题讨论】:

  • 为什么要使用模型

标签: mysql ruby-on-rails ruby excel ruby-on-rails-3


【解决方案1】:

这必须是视图表吗?除非你把它做成某种形式,否则如果不使用 javascript 解析 DOM(或者更确切地说访问 DOM 中的所有元素)并获取值并使用来自解析的信息向控制器发出请求,就无法做到这一点DOM。如果您可以制作某种形式的提交表单,那么您可以更轻松地将这些信息导出到 Excel 中。无论哪种方式,如果没有某种模型,确实没有其他方法可以做到这一点。

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多