Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中生成excel_Ruby On Rails_Ruby On Rails 3_Rubygems_Ruby On Rails Plugins - Fatal编程技术网

Ruby on rails 在rails中生成excel

Ruby on rails 在rails中生成excel,ruby-on-rails,ruby-on-rails-3,rubygems,ruby-on-rails-plugins,Ruby On Rails,Ruby On Rails 3,Rubygems,Ruby On Rails Plugins,我正在尝试用RubyonRails创建一个excel工作表。所以我使用了插件Rexcel。当我运行应用程序时,我得到以下错误 未初始化的常量Rexcel::工作簿::Builder 我添加了以下代码,然后这个错误 workbook = Rexcel::Workbook.new worksheet = workbook.add_worksheet("Customers") worksheet.add_line("name","test") headers['Content-Type'] =

我正在尝试用RubyonRails创建一个excel工作表。所以我使用了插件Rexcel。当我运行应用程序时,我得到以下错误

未初始化的常量Rexcel::工作簿::Builder

我添加了以下代码,然后这个错误

workbook = Rexcel::Workbook.new

worksheet = workbook.add_worksheet("Customers")

worksheet.add_line("name","test")

headers['Content-Type'] = "application/vnd.ms-excel"

headers['Content-Disposition'] = 'attachment; filename="excel-export.xlsx"'
headers['Cache-Control'] = 'max-age=0'
headers['pragma']="public"
workbook.build

如何解决这个问题?

我建议使用电子表格而不是Rexcel,因为它肯定更成熟,而且我在Rails 3中使用它时没有任何问题

整个过程将是:

book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => 'Customers'
sheet.row(0).concat %w{Name Country Acknowlegement}
book.write '/path/to/output/excel-file.xls'
+1到托马索普

我想补充一点。如果你像我一样,认为这是呈现数据,就像XML,HTML或JSON,你不想写磁盘。如果你考虑使用Heroku,它是只读的

我会将
book.write'/somepath..'
更改为

def xls
  .. your stuff .. 
  blob = StringIO.new("")
  book.write blob
  blob.string
end
然后,在控制器中执行以下操作:

send_data @customer.xls, :type => :xls, :filename => @customer.xls_file_name
记住在初始化器注释中添加mime类型,我刚刚意识到这些示例来自我的rails 2.3.10应用程序。也许它在rails 3中有所不同

Mime::Type.register "application/vnd.ms-excel", :xls

您是否正在寻找Rails3.x或其他版本的解决方案?另外,请发布错误的堆栈跟踪。是的,我正在rails 3.0中查找。完整跟踪如下..-----------------------------------------------FormadminController中的NameError#下载未初始化的常量Rexcel::工作簿::生成器供应商/plugins/Rexcel/lib/Rexcel.rb:210:in
build'app/controllers/formadmin\u controller.rb:192:in
download'actionpack(3.0.0)lib/action\u controller/metal/implicit\u render.rb:4:in
send\u action'actionpack(3.0.0)lib/abstract\u controller/base.rb:150:in
process\u action'actionpack(3.0.0)lib/action\u controller/metal/rendering.rb:11:in'process\u action'