3
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'})

关于如何下载它而无需先将其保存到服务器的任何建议?

4

2 回答 2

10

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

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
于 2016-01-02T10:58:36.807 回答
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 工作的人有所帮助!

于 2018-08-02T09:35:46.280 回答