我实施了一个CSV导出器,其工作方式如下:
CSV.generate
StringIO
分配文件)此过程效果很好!
我只是尝试将文件的内容类型更改为xlsx格式,但是使用paperclip时出现了内容欺骗错误。我也在努力设置它在Windows和Mac机器上运行的内容类型。我研究了一下,发现了以下插件:
CSV.generate
# data var filled up with with customers
CSV.generate(options) do |csv|
csv << [
'header 1',
'header 2'
]
data.each do |obj|
csv << [
obj.attr1,
obj.attr2,
]
end
end
file = StringIO.new(data) #mimic a real upload file
file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs
file.original_filename = "customer-export.#{customer_export.params['format']}" #assign filename in way that paperclip likes
file.content_type = content_type # text/plain for CSV and ? for XLSX
# the file can be assigned to an paperclip attachment and this works for now perfect
BTW:Ruby 2.2,Rails 4.1