下载工作簿而不将其保存到Rails中的磁盘rubyxl

时间:2016-01-02 07:56:03

标签: ruby-on-rails rubyxl

Rails 4.2
RubyXL

我使用RubyXL创建工作簿。我可以用以下内容写它:

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

然后使用以下内容下载:

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

有关如何下载它而不必先将其保存到服务器的任何建议吗?

2 个答案:

答案 0 :(得分:7)

您可以将工作簿直接转换为字符串:

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

答案 1 :(得分:1)

如果您在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工作的人可能会有所帮助!