如何使用 Rails 在 Excel 或 Word 中创建报告?
How to create reports in Excel or Word using Rails?
假设有一个 table 包含数据,并且您需要根据特定条件(例如按日期)生成 txt 或 xml 格式的报告。那么这是如何在 Rails 上实现的,例如使用 JavaScript?有什么文章或者更好的视频课吗?
你可以这样做:
在控制器中创建一个方法并在路由中将其定义为 post 请求。喜欢 -
def report
# some operation to generate the result
# assume that results is in @results instance variable
respond_to do |format|
format.xls {
response.headers['Content-Disposition'] = "attachment; filename=\"report.xls\""
}
end
end
在视图中添加一个表单,将新创建的路由作为表单操作。
<%= form_tag(report_path(format: :xls), method: 'post') do %>
# define a submit button which will hit the report method
<% end %>
最后创建一个 report.xls.erb
文件,其中包含从 @results
变量生成的 table。
希望对您有所帮助!
假设有一个 table 包含数据,并且您需要根据特定条件(例如按日期)生成 txt 或 xml 格式的报告。那么这是如何在 Rails 上实现的,例如使用 JavaScript?有什么文章或者更好的视频课吗?
你可以这样做:
在控制器中创建一个方法并在路由中将其定义为 post 请求。喜欢 -
def report
# some operation to generate the result
# assume that results is in @results instance variable
respond_to do |format|
format.xls {
response.headers['Content-Disposition'] = "attachment; filename=\"report.xls\""
}
end
end
在视图中添加一个表单,将新创建的路由作为表单操作。
<%= form_tag(report_path(format: :xls), method: 'post') do %>
# define a submit button which will hit the report method
<% end %>
最后创建一个 report.xls.erb
文件,其中包含从 @results
变量生成的 table。
希望对您有所帮助!