繁体   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