簡體   English   中英

在Rails中下載工作簿而不將其保存到磁盤rubyxl

[英]Downloading workbook without saving it to disk rubyxl in Rails

Rails 4.2
RubyXL

我正在使用RubyXL創建工作簿。 我可以使用以下方法將其寫入磁盤:

workbook.write("path/to/desired/Excel/file.xlsx")

然后使用以下內容下載它:

send_file(workbook.write(“ path / to / desired / Excel / file.xlsx”),options = {::文件名=>'myworkbook.xlsx',:disposition =>'附件'})

關於如何下載而不需要先將其保存到服務器的任何建議?

您可以將工作簿直接轉換為字符串:

def get_worksheet_as_string
  workbook = RubyXL::Workbook.new
  # Fill workbook here or leave as is to download empty
  send_data workbook.stream.string, filename: "myworkbook.xlsx",
                                    disposition: 'attachment'
end

如果您在Sinatra工作:

get '/download-xlsx/?' do
    content_type 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    attachment(filename = "filename.xlsx", disposition = :attachment)

    workbook = RubyXL::Workbook.new

    # Fill the workbook with data

    workbook.stream
end

認為這可能對在Sinatra工作的人有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM