直接下载xls文件而不通过Spreadsheet gem将其写入目录

时间:2023-02-05 09:56:11

I am using this Spreadsheet gem to export xls file.

我正在使用此Spreadsheet gem导出xls文件。

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)

但我想下载它而不是保存它。我怎么能修改代码,以便用户提示选择他的本地目录来保存文件? (如果不在服务器端保存副本,则更好)

Please help!

请帮忙!

3 个解决方案

#1


47  

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"

#2


2  

You could try this code

你可以尝试这个代码

book.write "data.xls"

send_file "/path/to/data.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false

# and then delete the file

File.delete("path/to/data.xls")

Passing :stream => false to send_file will instruct Rails to copy the entire file into memory before streaming, so using File.delete immediately after send_file would be fine since send_file returns immediately without waiting for the download to complete. Having said that, with very large files you may see some memory bottle necks depending on the amount of memory available.

传递:stream => false to send_file将指示Rails在流式传输之前将整个文件复制到内存中,因此在send_file之后立即使用File.delete就可以了,因为send_file会立即返回而无需等待下载完成。话虽如此,对于非常大的文件,您可能会看到一些内存瓶颈,具体取决于可用内存量。

HTH

HTH

#3


0  

The case happen on mybody. I used the ajax request by remote::true to export excel file, nothing display on browser without any error message on console. Delete the remote params from the form, it works well.

案件发生在我身上。我使用remote :: true的ajax请求导出excel文件,浏览器上没有任何显示,控制台上没有任何错误消息。从表单中删除远程参数,效果很好。

#1


47  

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"

#2


2  

You could try this code

你可以尝试这个代码

book.write "data.xls"

send_file "/path/to/data.xls", :type => "application/vnd.ms-excel", :filename => "data.xls", :stream => false

# and then delete the file

File.delete("path/to/data.xls")

Passing :stream => false to send_file will instruct Rails to copy the entire file into memory before streaming, so using File.delete immediately after send_file would be fine since send_file returns immediately without waiting for the download to complete. Having said that, with very large files you may see some memory bottle necks depending on the amount of memory available.

传递:stream => false to send_file将指示Rails在流式传输之前将整个文件复制到内存中,因此在send_file之后立即使用File.delete就可以了,因为send_file会立即返回而无需等待下载完成。话虽如此,对于非常大的文件,您可能会看到一些内存瓶颈,具体取决于可用内存量。

HTH

HTH

#3


0  

The case happen on mybody. I used the ajax request by remote::true to export excel file, nothing display on browser without any error message on console. Delete the remote params from the form, it works well.

案件发生在我身上。我使用remote :: true的ajax请求导出excel文件,浏览器上没有任何显示,控制台上没有任何错误消息。从表单中删除远程参数,效果很好。