You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/imports/base_import_excel.rb

29 lines
625 B

6 years ago
class BaseImportExcel < ApplicationImport
Error = Class.new(StandardError)
attr_reader :sheet
def initialize(path)
raise Error, '不支持的文件格式' unless path.end_with?('.xls') || path.end_with?('.xlsx')
begin
@sheet = Roo::Spreadsheet.open(path).sheet(0)
rescue Exception => ex
logger ex.message
ex.backtrace.each(&method(:logger))
raise Error, '打开文件失败'
end
check_sheet_valid!
end
def read_each(&block);end
private
def check_sheet_valid!;end
def raise_import_error(message)
raise Error, message
end
end