在rails中生成excel

发布于 2024-10-02 13:57:47 字数 539 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-10-09 13:57:47

我建议使用 Spreadsheet 而不是 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'

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:

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'
〃安静 2024-10-09 13:57:47

+1 给 tommasop

我想补充一下。如果您像我一样认为这是呈现数据,就像 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

+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..' to

def xls
  .. your stuff .. 
  blob = StringIO.new("")
  book.write blob
  blob.string
end

then, in the controller do

send_data @customer.xls, :type => :xls, :filename => @customer.xls_file_name

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.

Mime::Type.register "application/vnd.ms-excel", :xls
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文