Ruby on rails 无法使用acts_as_xlsx gem显示列标题
我正在使用acts_as_xlsx gem在rails中生成excel报告。我按照示例创建列标题,将它们添加到en.yml文件中,并在模型中包括:i18n=>true,但行为空。所有其他数据都显示正确 控制器:Ruby on rails 无法使用acts_as_xlsx gem显示列标题,ruby-on-rails,export-to-excel,Ruby On Rails,Export To Excel,我正在使用acts_as_xlsx gem在rails中生成excel报告。我按照示例创建列标题,将它们添加到en.yml文件中,并在模型中包括:i18n=>true,但行为空。所有其他数据都显示正确 控制器: def index @claims = Claim.all respond_to do | format | format.html # index.html.erb format.json { render :json => @claims }
def index
@claims = Claim.all
respond_to do | format |
format.html # index.html.erb
format.json { render :json => @claims }
format.xlsx {
xlsx_package = Claim.to_xlsx :name => "Claims",
:header_style => {:bg_color => "377",
:fg_color => "FF",
:sz => 16,
:alignment => { :horizontal => :center }}
begin
temp = Tempfile.new("claims.xlsx")
xlsx_package.serialize temp.path
send_data xlsx_package.to_stream.read, :filename => 'claims.xlsx', :type=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
ensure
temp.close
temp.unlink
end
}
end
end
acts_as_xlsx :columns => [:id, :business_unit_id, :created_at, :updated_at, :claim_number], :i18n => true
en:
activerecord:
attributes:
claims: Claims
claim:
id: "Claim Id"
business_unit_id: "Business Unit"
created_at: "Created at"
updated_at: "Updated at"
claim_number: "Claim Number"
型号:
def index
@claims = Claim.all
respond_to do | format |
format.html # index.html.erb
format.json { render :json => @claims }
format.xlsx {
xlsx_package = Claim.to_xlsx :name => "Claims",
:header_style => {:bg_color => "377",
:fg_color => "FF",
:sz => 16,
:alignment => { :horizontal => :center }}
begin
temp = Tempfile.new("claims.xlsx")
xlsx_package.serialize temp.path
send_data xlsx_package.to_stream.read, :filename => 'claims.xlsx', :type=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
ensure
temp.close
temp.unlink
end
}
end
end
acts_as_xlsx :columns => [:id, :business_unit_id, :created_at, :updated_at, :claim_number], :i18n => true
en:
activerecord:
attributes:
claims: Claims
claim:
id: "Claim Id"
business_unit_id: "Business Unit"
created_at: "Created at"
updated_at: "Updated at"
claim_number: "Claim Number"
En.yml:
def index
@claims = Claim.all
respond_to do | format |
format.html # index.html.erb
format.json { render :json => @claims }
format.xlsx {
xlsx_package = Claim.to_xlsx :name => "Claims",
:header_style => {:bg_color => "377",
:fg_color => "FF",
:sz => 16,
:alignment => { :horizontal => :center }}
begin
temp = Tempfile.new("claims.xlsx")
xlsx_package.serialize temp.path
send_data xlsx_package.to_stream.read, :filename => 'claims.xlsx', :type=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
ensure
temp.close
temp.unlink
end
}
end
end
acts_as_xlsx :columns => [:id, :business_unit_id, :created_at, :updated_at, :claim_number], :i18n => true
en:
activerecord:
attributes:
claims: Claims
claim:
id: "Claim Id"
business_unit_id: "Business Unit"
created_at: "Created at"
updated_at: "Updated at"
claim_number: "Claim Number"
它不起作用的原因是您使用的en.yml文件是复数形式的索赔,您应该使用索赔
en:
activerecord:
claim:
id: "Claim Id "
business_unit_id: "Business Unit "
created_at: "Created at "
updated_at: "Updated at "
claim_number: "Claim Number "
我认为i8n应该使用的开关实际上是基于最新文档的“activerecord.attributes”
:i18n => "activerecord.attributes"
另一方面,我认为您需要在标题后添加空格,否则表单中显示的标题将无法正确隔开。好的,我意识到问题在于Mac上的预览(因为我没有excel)。当我将文档上传到google drive时,我在列标题中看到了文本(注意:我还删除了:fg_color=>“FF”,因此我的字体在黄色背景上不是白色的)。即使进行了这些更改,我仍然不确定为什么我可以使用预览在其他行中看到数据,但在列标题行中看不到数据。但是数据正确地填充了字段。