Rails导出的xls文件在excel和libre office上打开不同

时间:2013-07-14 20:51:36

标签: 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.generate(options) do |csv|
      csv << column_names
      all.each do |tour|
        csv << tour.attributes.values_at(*column_names)
      end
    end
  end
end

锦标赛控制员:

  def show
    @tournament = Tournament.includes(:pilots => :country).find(params[:id])
    @pilots = @tournament.pilots
    @tours = @tournament.tours.includes(:pilots => :country)

    respond_to do |format|
      format.html
      format.xls
    end
  end

显示视图中的链接:

= link_to "Download xls", admin_tournament_path(format: "xls"), :class => "btn"

我的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轮..

1 个答案:

答案 0 :(得分:1)

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

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

希望它有所帮助,谢谢。