【问题标题】:Rails exported xls file opens different on excel and libre officeRails 导出的 xls 文件在 excel 和 libre office 上的打开方式不同
【发布时间】:2013-07-14 20:51:36
【问题描述】:

我的应用程序知道如何导出 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 轮..

【问题讨论】:

    标签: ruby-on-rails csv export-to-excel xls


    【解决方案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 %>
    

    希望对你有帮助,谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多