未初始化的常量ActionView :: CompiledTemplates :: WriteXLSX

时间:2015-11-09 06:11:16

标签: ruby-on-rails ruby excel ruby-on-rails-4

我正在尝试使用“write_xlsx_rails”gem将数据导出到rails中的Excel电子表格。

控制器:

def download
   render xlsx: "download", disposition: "attachment", filename:   "my_new_filename.xlsx"
end  

查看(download.xlsx.wxlsx):

    # Add a worksheet
    worksheet = workbook.add_worksheet

    # Add and define a format
    format = workbook.add_format # Add a format
    format.set_bold
    format.set_color('red')
    format.set_align('center')

     # Write a formatted and unformatted string, row and column notation.
     col = row = 0
     worksheet.write(row, col, "Hi Excel!", format)
     worksheet.write(1,   col, "Hi Excel!")

     # Write a number and a formula using A1 notation
     worksheet.write('A3', 1.2345)
     worksheet.write('A4', '=SIN(PI()/4)')

但我得到的错误是这样的:

  

未初始化的常量ActionView :: CompiledTemplates :: WriteXLSX

1 个答案:

答案 0 :(得分:0)

根据write_xlsx_rails文档,如果指定文件名,则必须使用格式,例如。

def download
  respond_to do |format|      
   format.xlsx{render xlsx: "download", disposition: "attachment", filename: "my_new_filename.xlsx"}
  end
end