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
精彩评论