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