开发者

Rails "can't convert Spreadsheet::Workbook into String"

I'm trying to use the spreadsheet gem in my Rails app, but I get the following error:

can't convert Spreadsheet::Workbook into String

This is my code:

require 'spreadsheet'

def create_spreadsheet_from_array(array)
   Spreadsheet.client_encoding = 'UTF-8'
   book = Spreadsheet::Workbook.new
   ...
end

What am I doing wr开发者_JAVA百科ong?


My best guess from your limited details is that your code trying to output the rendered spreadsheet file in a view template. You need to use send_file in your controller. Here is a simple example (but I don't really recommend having the spreadsheet code in the controller):

class MyController < ApplicationController
  def download_xls
    filename = "example.xls"
    tempfile = Tempfile.new(filename)
    workbook = Spreadsheet::Workbook.new
    ...
    workbook.write(tempfile.path)
    send_file tempfile.path, :filename => filename
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜