Ruby on rails Rails导出的xls文件在excel和libre office上打开的方式不同

Ruby on rails Rails导出的xls文件在excel和libre office上打开的方式不同,ruby-on-rails,csv,export-to-excel,xls,Ruby On Rails,Csv,Export To Excel,Xls,我的应用程序知道如何导出xls文件,但导出的文件仅在使用LibreOffice打开时显示所有信息,Microsoft Excel仅显示整个文件的一部分 我的旅行班: class Tour < ActiveRecord::Base belongs_to :tournament has_and_belongs_to_many :pilots, :join_table => :rounds def self.to_csv(options = {}) CSV.gene

我的应用程序知道如何导出xls文件,但导出的文件仅在使用LibreOffice打开时显示所有信息,Microsoft Excel仅显示整个文件的一部分

我的旅行班:

class Tour < ActiveRecord::Base
  belongs_to :tournament
  has_and_belongs_to_many :pilots, :join_table => :rounds


  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << column_names
      all.each do |tour|
        csv << tour.attributes.values_at(*column_names)
      end
    end
  end
end
显示视图中的链接:

= link_to "Download xls", admin_tournament_path(format: "xls"), :class => "btn"
和my show.xls.erb:

<?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">
    <% @tours.each_with_index do |tour, index| %>
      <Table>
        <Row></Row>
        <Row>
          <Cell><Data ss:Type="String"><%= "Tour #{index + 1}" %></Data></Cell>
        </Row>
        <Row>
          <Cell><Data ss:Type="String">#</Data></Cell>
          <Cell><Data ss:Type="String">Pilot Name</Data></Cell>
          <Cell><Data ss:Type="String">Pilot Country</Data></Cell>
        </Row>
      <% tour.pilots.each_with_index do |pilot, index| %>
        <Row>
          <Cell><Data ss:Type="Number"><%= index + 1 %></Data></Cell>
          <Cell><Data ss:Type="String"><%= pilot.name %></Data></Cell>
          <Cell><Data ss:Type="String"><%= pilot.country.name %></Data></Cell>
        </Row>
      <% end %>
      </Table>
    <% end %>
  </Worksheet>
</Workbook>

#
飞行员姓名
试点国家

当我用Microsoft excel打开下载的文件时,它只显示第一轮,LibreOffice显示所有现有的13轮。

事实上,我很惊讶LibreOffice可以显示它,因为根据,它说一个表元素只有一个实例对单个工作表有效。:

  .....
  <xsd:element name="Table" type="TableType" minOccurs="0">
        <xsd:annotation>
            <xsd:documentation>Defines the table to contain the cells of the current worksheet. Only one instance of a Table element is valid for a single worksheet.</xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    .....
。。。。。
定义包含当前工作表单元格的表。一个表元素只有一个实例对单个工作表有效。
.....
因此,我建议您只打印一个表元素中的行,如下所示:

 <Table>
 <% @tours.each_with_index do |tour, index| %>      
    <Row></Row>
    <Row>
      <Cell><Data ss:Type="String"><%= "Tour #{index + 1}" %></Data></Cell>
    </Row>
    <Row>
      <Cell><Data ss:Type="String">#</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Name</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Country</Data></Cell>
    </Row>
  <% tour.pilots.each_with_index do |pilot, index| %>
    <Row>
      <Cell><Data ss:Type="Number"><%= index + 1 %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.name %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.country.name %></Data></Cell>
    </Row>
  <% end %> 
<% end %>
</Table>
<% @tours.each_with_index do |tour, index| %>
  <Worksheet ss:Name="<%= "Tour #{index + 1}-Sheet" %>">
  <Table>
    <Row></Row>
    <Row>
      <Cell><Data ss:Type="String"><%= "Tour #{index + 1}" %></Data></Cell>
    </Row>
    <Row>
      <Cell><Data ss:Type="String">#</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Name</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Country</Data></Cell>
    </Row>
  <% tour.pilots.each_with_index do |pilot, index| %>
    <Row>
      <Cell><Data ss:Type="Number"><%= index + 1 %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.name %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.country.name %></Data></Cell>
    </Row>
  <% end %>
  </Table>
  </Worksheet>
<% end %>

#
飞行员姓名
试点国家
或者您可以将它们放入不同的工作表中,如下所示:

 <Table>
 <% @tours.each_with_index do |tour, index| %>      
    <Row></Row>
    <Row>
      <Cell><Data ss:Type="String"><%= "Tour #{index + 1}" %></Data></Cell>
    </Row>
    <Row>
      <Cell><Data ss:Type="String">#</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Name</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Country</Data></Cell>
    </Row>
  <% tour.pilots.each_with_index do |pilot, index| %>
    <Row>
      <Cell><Data ss:Type="Number"><%= index + 1 %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.name %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.country.name %></Data></Cell>
    </Row>
  <% end %> 
<% end %>
</Table>
<% @tours.each_with_index do |tour, index| %>
  <Worksheet ss:Name="<%= "Tour #{index + 1}-Sheet" %>">
  <Table>
    <Row></Row>
    <Row>
      <Cell><Data ss:Type="String"><%= "Tour #{index + 1}" %></Data></Cell>
    </Row>
    <Row>
      <Cell><Data ss:Type="String">#</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Name</Data></Cell>
      <Cell><Data ss:Type="String">Pilot Country</Data></Cell>
    </Row>
  <% tour.pilots.each_with_index do |pilot, index| %>
    <Row>
      <Cell><Data ss:Type="Number"><%= index + 1 %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.name %></Data></Cell>
      <Cell><Data ss:Type="String"><%= pilot.country.name %></Data></Cell>
    </Row>
  <% end %>
  </Table>
  </Worksheet>
<% end %>

#
飞行员姓名
试点国家

希望有帮助,谢谢