简体   繁体   English

将Ruby on Rails HTML表导出到.xls文件

[英]Export Ruby on Rails HTML table to .xls file

I have the following table: 我有下表:

<tr>
    <th colspan = "18">  student info</th>
    <th colspan="10">class info</th>

</tr>


 <tr>
        <th colspan="1">First Name</th>
        <th colspan="1">Middle Name</th>
        <th colspan="1">Last Name</th>

        ....
</tr>

<tr>
          <td colspan="1"><%= link_to student.first_name,:controller => 'acme_modificationss' , :action=>'profile', :id => student.id %></td>
          <td colspan="1"><%= student.middle_name %></td>
          <td colspan="1"><%= student.last_name %></td>
          <td colspan="1"><%= student.first_name %></td>
          <td colspan="1"><%=m_first_name%></td>

.....

I need to export the same table to .xls file. 我需要将同一张表导出到.xls文件。 So I added a new action to the controller: 因此,我向控制器添加了一个新操作:

    def document_xls
             ....
        respond_to do |format|
          format.xls

        end

      end

Then I added document_xls view: 然后我添加了document_xls视图:

<?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>
          <Row>
                <Cell><Data ss:Type="String">Student Info</Data></Cell>
                <Cell><Data ss:Type="String">Class info</Data></Cell>
             ....
            </Row>
            <Row>
                <Cell><Data ss:Type="String">First Name</Data></Cell>
                <Cell><Data ss:Type="String">Middle Name</Data></Cell>
                <Cell><Data ss:Type="String">Last Name</Data></Cell>
...

This generates a file with type file. 这将生成文件类型为file的文件。 But I want to generate this file as .xls, so I tried to add this to my controller action: 但是我想将此文件生成为.xls,因此我尝试将其添加到控制器操作中:

  format.xls{ send_data @students, :type => 'application/vnd.ms-excel', :filename => 'students.xls' }

But I got this error: 但是我得到了这个错误:

NoMethodError (undefined method `bytesize' for #) NoMethodError(#的未定义方法“ bytesize”)

Also, I need the excel table headers to be merged in more than one cell, is there a way to do this? 另外,我需要将excel表标题合并到多个单元格中,有没有办法做到这一点?

One easier way to fix this is send content as HTML to excel. 解决此问题的一种简便方法是将内容作为HTML发送到excel。 Excel is able to parse it and convert by his own, you just need to send the right headers. Excel能够对其进行解析和转换,您只需要发送正确的标头即可。 first, have your table in a partial view, then, in your controller create a method like this: 首先,将表放在局部视图中,然后在控制器中创建如下方法:

  def export
    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Content-Disposition'] = 'attachment; filename="report.xls"'
    headers['Cache-Control'] = ''
    @data = self.send params[:type]
    render layout: false
  end

3 first lines export the excel file, so, the next line send the data receiving like type method the name of the method that retrieve your data to html and excel view, and render: false show you an empty layout jus loading your data. 前三行输出excel文件,因此,下一行将接收到的数据(如type method)发送到html和excel视图,然后将其检索数据的方法的名称发送给html和excel视图,然后进行渲染:false向您显示一个空布局,表示正在加载数据。

in your routes something like 在您的路线中,类似

get 'excel/stats/:type', to: 'stats#export', as: 'excel_stat'

In this example we have the stats controller with many different types of stats, and in a view 'export' in your controller, you can render the partial view like this 在此示例中,我们的stats控制器具有许多不同类型的stats,并且在控制器的“导出”视图中,您可以像这样呈现部分视图

export.html.haml : export.html.haml:

= render params[:type]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM