首先下个插件,在你项目根目录
ruby script/plugin install http://svn.napcsweb.com/public/excel
此时就会唉vendor下多个文件夹,注意。里面有个excel.rb,这就是我们的核心方法
然后转到我们的控制器
#导出Excel开始
def export
@ala_users = User.find(:all)
@count = Record.count(:conditions => [' ala_organization_id = ? and ala_user_id = ? ',session[:organization_id],session[:user_id]])
@count_pass = Record.count(:conditions => [' ala_organization_id = ? and ala_user_id = ? and status = ? ',session[:organization_id],session[:user_id],'6'])
array = Array.new
for i in 0..@ala_users.length-1
item = OrderedHash.new
item["姓名"] = @ala_users[i].name
item["部门"] = @ala_users[i].ala_department.name if @ala_users[i].ala_department
item["职位"] = @ala_users[i].positions
item["学时"] = @ala_users[i].learning_times
item["学分"] = @ala_users[i].learning_credits
item["报名课程数"] = @count[i]
item["通过课程数"] = @count_pass[i]
array << item
end
e = Excel::Workbook.new
e.addWorksheetFromArrayOfHashes("Books info", array)
headers['Content-Type'] = "application/vnd.ms-excel"
headers['Content-Disposition'] = 'attachment; filename="excel-export.xls"'
headers['Cache-Control'] = ''
render :text=>e.build
end
现在就ok了,但是注意OrderedHash这个hash,这个hash是有序的,爽吧!别慌,需要在控制器里面导入
require "active_support"
include ActiveSupport
OrderedHash才可已用
分享到:
相关推荐
这个简单的插件使您能够调用to_xls到Rails的数组集合。 数组元素支持对象:ActiveRecord,Mongid,哈希。 在您的Gemfile中: gem 'to_xls-rails' # Last officially released gem # gem "to_xls-rails", :git => ...
Ruby on Rails中文指南
Ruby On Rails中文教材(PDF)
敏捷Rails中文教程 敏捷Rails中文教程 敏捷Rails中文教程
poi导出excel需要的jar
rails指南 中文版
rails-assets, 在 Rails 中,资产管理的解决 Rails 资产 Bundler 到 Bower 代理本自述文件涉及项目的开发方面。 访问站点了解如何在你的应用程序中使用 Rails 资产。 插件开发设置git clone git@github.com:tenex/r
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入...
在过去的几年中,《Ruby on Rails Tutorial》这本书被视为介绍使用 Rails 进行 Web 开发的先驱者。 在这个全球互联的世界中,计算机编程和 Web 应用程序开发都在迅猛发展,我很期待能为中国的开发者提供 Ruby on ...
Simple Form - 轻松处理Rails表单
rails_email_preview, 在 Rails 中,预览和编辑应用程序邮件程序模板 Rails 电子邮件预览 使用这里 Rails 引擎在浏览器中预览电子邮件。 兼容 Rails 4.2 。电子邮件审阅: 所有电子邮件预览的列表: 代表有两个主题...
Xport 表格数据导出到Excel,CSV等产品特点- column :name, group: :project 列宽- column :name, width: 10 列标题标题- column :name, header: "Full name" 列类型- column :name, type: :string 列样式- column :...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
shoppe-example, 在 Rails 中使用Shoppe平台的示例存储实现 这是一个 Rails 应用程序,它使用 Shoppe 构建。 它具有一个可以爱的设计,以充分演示Shoppe平台提供的功能以及它在 Rails 应用程序中的。 正在启动要开始...