在rails中生成excel
我正在尝试在 ruby on Rails 中创建一个 Excel 工作表。所以我使用了Rexcel这个插件。当我运行该应用程序时,我收到以下错误。
未初始化的常量 Rexcel::Workbook::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
如何解决这个问题?
I am trying to create an excel sheet in ruby on rails. So I used the plugin Rexcel. When I am running the application I am getting the following error.
uninitialized constant Rexcel::Workbook::Builder
I had added the following code, then this error hitting
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
How to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 Spreadsheet 而不是 Rexcel,因为它绝对更成熟,而且我将它与 Rails 3 一起使用没有任何问题。
这是文档。
总体流程是:
I would advice to use Spreadsheet instead of Rexcel because is definitely more mature and I'm using it with Rails 3 with no problems.
Here is the documentation.
The overall process would be:
+1 给 tommasop
我想补充一下。如果您像我一样认为这是呈现数据,就像 xml、html 或 json 一样,那么您不想写入光盘。如果您考虑使用 Heroku,它是只读的。
我会将
book.write '/somepath..'
更改为,然后在控制器中记住
在初始化程序中添加 mime 类型注意,我刚刚意识到这些示例来自我的 Rails 2.3。 10 个应用程序。也许 Rails 3 有所不同。
+1 to tommasop
I'd like to add. If you, like me, you consider this as presenting data, just like xml, html or json, you don't want to write to disc. Heroku is read-only if you're considering using that.
I would change the
book.write '/somepath..'
tothen, in the controller do
remember to add the mime type in your initializer NOTE, I just realized that these examples are from my rails 2.3.10 app. Maybe it differs in rails 3.