find-classroom/app/models/excel.rb

49 lines
1.2 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

=begin
Date: 2016.12.28
创建基于Importex::Base的类
使用导入excel的ruby插件 importex
gem install importex
或者使用
./script/plugin install git://github.com/ryanb/importex.git
=end
require 'importex'
class Excel <Importex::Base
column "Name",:required=> true
column "class",:required=> true
column "Grades",:type=> Integer
column "Date",:type=> Date
column "Discontinued",:type=> Boolean
end
#导入excel文件
Excel.import("course.excel")
#使用导入数据
#all代表所有的数据各个字段作为hash
excel = Excel.all
excel.first["Discontinued"] # => false
#数据验证和容错处理
#当excel的数据格式不能处理的时候或者数据格式不符合时
begin
Excel.import()
rescue Importex::ImportError => e
puts e.message
end
#允许导入的字段指向Ruby对象
# 例如下面的例子可以增加一个字段表示上面导入Product的类型情况
class Category < ActiveRecord::Base
def self.importex_value(str)
find_by_name!(str)
rescue ActiveRecord::RecordNotFound
raise Importex::InvalidCell, "No category with that name."
end
end
class Product < Importex::Base
column "Category", :type => Category
end