我应该在哪里放置生成 Excel 电子表格的代码?

发布于 2024-08-05 12:22:54 字数 461 浏览 0 评论 0原文

我正在使用 spreadsheet gem 生成本机 Excel 文件。这不是 CSV、XML 文件。使用普通的 Ruby 代码来创建该文件。生成的 Excel 文件(保存在 StringIO 中)使用 send_data 方法转发到客户端。我需要 send_data 方法,因为它的参数如 disposition

Excel 的数据是在控制器方法中检索的,就像普通的 HTML、JS 请求一样。不过,我将生成电子表格的代码放置在控制器受保护的方法中。不是我应该看到的。

是否有符合 MVC 设计模式的优雅解决方案来解决上述问题?

更新:对于上述问题,没有流行且被所有人接受的解决方案,但至少我知道所有可能的想法。

I am using spreadsheet gem to generate native Excel file. This is not CSV, XML file. Ordinary Ruby code is used to create the file. The generated Excel file (kept in StringIO) is forwarded to a client using send_data method. I need send_data method because of its parameters like disposition.

The data for the Excel is retrieved in controller method just like for ordinary HTML, JS requests. However I placed the code to generate the spreadsheet in controller protected method. Not in a view as I should.

Is there an elegant solution to above problem compliant with MVC design pattern?

Update: There is no popular and accepted by all solution to above problem but at least I know all possible ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

匿名。 2024-08-12 12:22:54

lib 目录是存放严格意义上不属于 MVC 结构一部分的代码的地方,但多个模型、视图或控制器都需要这些代码。需要时可以通过 require 引入它。

但是,如果您只需要单个控制器中的代码,那么您最好将其放入该控制器的帮助程序中。这样它就会自动加载并触手可及。另外,它是有道理的:它是帮助特定控制器的代码。

无论哪种方式,都不要将其留在控制器中或尝试将其插入您的视图中。

The lib directory is meant as a place for code that isn't strictly part of the MVC structure, but will be needed by multiple models, views, or controllers. It can be brought in with a require when needed.

However, if you only need the code in a single controller, you'd be just as well off putting it into that controller's helper. That way it's auto-loaded and at your fingertips. Plus, it makes sense: it's code to help a specific controller.

Either way, don't leave it in your controller or try to wedge it into your view.

遗失的美好 2024-08-12 12:22:54
  1. 在 lib 文件夹中创建一个 excel 库,其中包含 xls 生成例程以及覆盖 ActionController 的 render 方法的方法。
  2. 在应呈现为 xls 的模型中,实现名为 to_excel 方法的方法,该方法生成可提供给 xls 例程的哈希值。
  3. 这样做,你会得到一些真正“Railsy”的东西。在你的控制器中你只需调用

    渲染:xls => @model

  1. Create an excel library in your lib folder in which you include your xls generation routine as well as a method that overrides ActionController's render method.
  2. In a model that should be rendered as xls implement a method called to_excel method which generates a hash that you can provide to your xls routine.
  3. Doing it this way, you'll get something really "Railsy". In your controller you'll just call

    render :xls => @model

月竹挽风 2024-08-12 12:22:54

我今天刚刚为我的应用程序做了这个,希望这对 excel o/p 有帮助;从未使用过任何插件

控制器:
默认导出
pr = Program.find(会话[:pr_id])
headers['Content-Type']="application/vnd.ms-excel"
headers['Content-Dispositon']='attachment;filename="report.xls"'
@voucherdatas = Voucherdata.find_all_by_pr_name(pr.pr_name)
最终

视图:export.html.erb

manager

"reports/voucherdatas", :object =>@voucherdatas %>

paths.rb
map.resources :reports ,:collection =>{:export =>:get}

无论您想要链接在哪里,都给出

link_to "Export As Excel", export_reports_url, :popup=>true

i just did this today for my app hope this helps for an excel o/p ; never used any plugin

controller:
def export
pr = Program.find(session[:pr_id])
headers['Content-Type']="application/vnd.ms-excel"
headers['Content-Dispositon']='attachment;filename="report.xls"'
@voucherdatas = Voucherdata.find_all_by_pr_name(pr.pr_name)
end

view: export.html.erb

manager

"reports/voucherdatas", :object =>@voucherdatas %>

routes.rb
map.resources :reports ,:collection =>{:export =>:get}

whereever u want the link give

link_to "Export As Excel", export_reports_url, :popup=>true

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