直接下载xls文件,而无需通过Spreadsheet gem将其写入目录 [英] Direct downloading a xls file without writing it to the directory by Spreadsheet gem
本文介绍了直接下载xls文件,而无需通过Spreadsheet gem将其写入目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此电子表格 gem来导出xls文件.
I am using this Spreadsheet gem to export xls file.
我的控制器中包含以下代码:
I have the following codes in my controller:
def export
@data = Data.all
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => "data"
contruct_body(sheet, @data)
book.write "data.xls"
end
这样,我可以填写数据并将其保存在根目录中.
In this way, I can fill in the data and save it in the root directory.
但是我要下载而不是保存它.如何修改代码,以便用户提示选择其本地目录以保存文件? (最好是不将副本保存在服务器端)
But I want to download it instead of save it. How could I modify the code so that the user prompted to select his local directory to save the file? (better if without saving a copy in the server side)
请帮助!
推荐答案
您可以将其发送到浏览器,而无需将其保存为本地文件,如下所述
You can send it to the browser without saving it as a local file at all as follows
spreadsheet = StringIO.new
book.write spreadsheet
send_data spreadsheet.string, :filename => "yourfile.xls", :type => "application/vnd.ms-excel"
这篇关于直接下载xls文件,而无需通过Spreadsheet gem将其写入目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文