Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将Ruby on Rails HTML表导出到.xls文件_Ruby On Rails_Ruby_Excel_Html Table_Xls - Fatal编程技术网

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

Ruby on rails 将Ruby on Rails HTML表导出到.xls文件,ruby-on-rails,ruby,excel,html-table,xls,Ruby On Rails,Ruby,Excel,Html Table,Xls,我有下表: <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&g

我有下表:

<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>

.....

然后我添加了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>
...
但我有一个错误:

NoMethodError(未定义的方法'bytesize'表示#)


另外,我需要将excel表格标题合并到多个单元格中,有没有办法做到这一点?

一种更简单的解决方法是将内容作为HTML发送到excel。Excel能够自己解析和转换,您只需要发送正确的标题。首先,将表置于局部视图中,然后在控制器中创建如下方法:

  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行导出excel文件,因此,下一行发送数据接收,如type method,将检索数据的方法的名称发送到html和excel视图,并呈现:false显示加载数据的空布局

在你的路线上

get 'excel/stats/:type', to: 'stats#export', as: 'excel_stat'
在这个例子中,我们有一个stats控制器,它具有许多不同类型的stats,在控制器中的视图“导出”中,您可以像这样渲染局部视图

export.html.haml:

= render params[:type]

解决这个问题的一个简单方法是将内容以HTML格式发送到excel。Excel能够自己解析和转换,您只需要发送正确的标题。首先,将表置于局部视图中,然后在控制器中创建如下方法:

  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行导出excel文件,因此,下一行发送数据接收,如type method,将检索数据的方法的名称发送到html和excel视图,并呈现:false显示加载数据的空布局

在你的路线上

get 'excel/stats/:type', to: 'stats#export', as: 'excel_stat'
在这个例子中,我们有一个stats控制器,它具有许多不同类型的stats,在控制器中的视图“导出”中,您可以像这样渲染局部视图

export.html.haml:

= render params[:type]

您尝试过使用而不是吗?当我使用format.xls
{send\u file document\u xls,:type=>'application/vnd.ms excel'}
时,我遇到了以下错误:
SystemStackError(堆栈级别太深):
我想我传递参数document\u xls时出错了,这是相同的操作,所以我不知道如何在send_fileremove all
respond_to do | f |中传递它。。。结束
在操作结束时阻塞并放置
发送文件(…)。
这会产生以下错误:
类型错误(无法将nil转换为字符串):
您是否尝试过使用而不是?当我使用format.xls
{send\u file document\u xls时,键入=>'application/vnd.ms excel'}
我遇到以下错误:
SystemStackError(堆栈级别太深):
我认为我传递参数文档是错误的,它是同一个操作,所以我不知道如何在send_fileremove all
respond_to do | f |…end
块并将
send_file(…)传递到操作结束处。
这给出了以下错误:
类型错误(无法将nil转换为字符串):