繁体   English   中英

如何从sidekiq在xls文件中添加标题名称?

[英]How to add Header names in xls files from sidekiq?

File.open("#{Rails.root}/tmp/report/sample.xls", 'w')
 { |f| f.write
 ( @policies) 
 } 

上面的代码在 tmp 文件夹中创建 xls 文件工作正常,但我想要该 xls 文件中的标题名称,如何在该代码中传递标题名称? 这段代码是用 sidekiq 编写的,用于后台进程。 请任何人建议我

sampl.xls 文件:

[{"id"=>1, "office"=>" ", "ordernumber"=>"100", "code"=>"NA", "type"=>"I", "prev_order_number"=>nil, "insured_name"=>"sree"}]

使用 CSV 来节省资源。 Excel 将毫无问题地打开它。

csv_file = CSV.generate do |csv|
    csv << @policies.first.keys
    @policies.each do |policy|
        csv << policy.values
    end
end

File.write("#{Rails.root}/tmp/report/sample.xls", csv_file)

以前的回答

如果您提供的代码完全符合您的要求,请尝试以下操作:

headers = {col1: "id", col2: "office", col3: "ordernumber", col4: "code", col5: "type", col6: "prev_order_number", col7: "insured_name"}

File.open("#{Rails.root}/tmp/report/sample.xls", 'w') do |f|
  f.write @policies.unshift(headers)
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM