Permalink
Browse files

导入excel && 数据库迁移

  • Loading branch information...
1 parent 10c9108 commit 8f7d00328d7b0642c96ed364b159f462ad7f47e8 @xEasy xEasy committed Mar 23, 2012
View
@@ -4,3 +4,4 @@ log/*.log
*.swp
tmp/**/*
config/database.yml
+public/system
View
@@ -14,6 +14,8 @@ gem 'rd_searchlogic', '3.0.1', :require => 'searchlogic'
gem 'kindeditor'
gem 'rmagick'
gem 'paperclip', '~> 2.3'
+gem 'roo', '1.3.11'
+
# Use unicorn as the web server
# gem 'unicorn'
View
@@ -1,6 +1,9 @@
GEM
remote: http://ruby.taobao.org/
specs:
+ GData (0.0.4)
+ builder (>= 2.1.2)
+ hoe (>= 1.2.1)
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (= 3.0.3)
@@ -40,8 +43,12 @@ GEM
erubis (2.6.6)
abstract (>= 1.0.0)
gem_plugin (0.2.3)
+ hoe (2.12.3)
+ rake (~> 0.8)
+ hpricot (0.8.4)
i18n (0.6.0)
kindeditor (0.0.3)
+ libxml-ruby (2.0.9)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -81,6 +88,16 @@ GEM
rd_searchlogic (3.0.1)
activerecord (>= 3.0.0)
rmagick (2.13.1)
+ roo (1.3.11)
+ GData (>= 0.0.4)
+ hpricot (>= 0.6)
+ libxml-ruby (>= 1.1.3)
+ rubyzip (>= 0.9.1)
+ spreadsheet (>= 0.6.4)
+ ruby-ole (1.2.11.1)
+ rubyzip (0.9.4)
+ spreadsheet (0.6.5.7)
+ ruby-ole (>= 1.0)
thor (0.14.6)
treetop (1.4.10)
polyglot
@@ -102,3 +119,4 @@ DEPENDENCIES
rake (= 0.8.7)
rd_searchlogic (= 3.0.1)
rmagick
+ roo (= 1.3.11)
@@ -0,0 +1,21 @@
+# -*- encoding : utf-8 -*-
+class MasterController < ApplicationController
+ def index
+ end
+
+ def import
+ respond_to do |format|
+ format.html do
+ begin
+ @import = ImportMaster.create(params[:master])
+ MasterImporter.create(params[:model_name], @import.attachment.url.split("?")[0])
+ @import.destroy
+ render :text => true
+ rescue => e
+ @import.destroy
+ render :json => { :error_msg => e.message, :success => false }, :content_type => 'text/html'
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+class ImportMaster < ActiveRecord::Base
+ has_attached_file :attachment
+end
View
@@ -16,5 +16,6 @@ def total_score
def grade
'优秀'
end
+
end
@@ -13,6 +13,7 @@
<%= javascript_include_tag 'PF/public/init.js' %>
<%= javascript_include_tag 'PF/util/util.js' %>
+ <%= javascript_include_tag 'PF/util/master_import.js' %>
<%= javascript_include_tag 'Jquery/jquery.js' %>
<%= javascript_include_tag 'Jquery/interface.js' %>
View
@@ -1,6 +1,7 @@
require File.expand_path('../boot', __FILE__)
require 'rails/all'
+require File.expand_path('../../lib/usr/master_importer', __FILE__)
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
View
@@ -1,4 +1,7 @@
Pf::Application.routes.draw do
+ match "/master" => "master#index"
+ match "/master/import" => "master#import"
+
resources :homes do
collection do
get "index"
@@ -0,0 +1,16 @@
+class CreateImportMasterTable < ActiveRecord::Migration
+ def self.up
+ create_table "import_masters", do |t|
+ t.string "attachment_file_name"
+ t.string "attachment_content_type"
+ t.integer "attachment_file_size"
+ t.datetime "attachment_updated_at"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+ end
+
+ def self.down
+ drop_table :import_masters
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120322072226) do
+ActiveRecord::Schema.define(:version => 20120323131334) do
create_table "classes", :force => true do |t|
t.string "name"
@@ -51,6 +51,15 @@
t.string "remark"
end
+ create_table "import_masters", :force => true do |t|
+ t.string "attachment_file_name"
+ t.string "attachment_content_type"
+ t.integer "attachment_file_size"
+ t.datetime "attachment_updated_at"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "people", :force => true do |t|
t.string "name"
t.string "phone"
View
Binary file not shown.
View
@@ -0,0 +1,104 @@
+# -*- encoding : utf-8 -*-
+require "roo"
+
+module MasterImporterModule
+ def column_type_change model_name, column_name, value
+ r = nil
+ case model_name.constantize.columns_hash[column_name].type
+ when :string then r = value.to_s.strip
+ when :integer then r = value.to_i
+ when :float then r = value.to_f
+ end
+ r
+ end
+
+ def foreign_key xls, row_index, attach_column
+ conditions = {}
+ model_name = attach_column["model_name"].split("_").collect(&:capitalize).join
+ attach_column["columns"].each do |column_name, index|
+ conditions[column_name] = column_type_change(model_name, column_name, xls.cell(row_index, index))
+ end
+ result = model_name.constantize.where(conditions)
+ if result.blank?
+ error_msg = "主档#{attach_column["model_name"]}不存在\n"
+ attach_column["columns"].each do |key, value|
+ error_msg += "#{key} = #{xls.cell(row_index, value)} \n"
+ end
+ raise error_msg
+ else
+ result.first.id
+ end
+ end
+
+ def find_excel path
+ Excel.new(Rails.root.to_s + "/public" + path)
+ end
+
+ def divide xls
+ #columns = { "supplier_id" => { "columns" => { "number" => 12, ... }, "model_name" => "company" }, "number" => 1, ... }
+ columns = {}
+ index = 1
+
+ while(index <= xls.last_column)
+ field = xls.cell((xls.first_row + 1), index)
+ column = { field => index }
+ model_name = ""
+
+ unless field.match(/#\w+/).blank?
+ raise "'#'格式内不能存在'$'符号" if(!field.match(/^\$/).blank?)
+ tmp, field = field.split('#')
+ #{ "columns" => { tmp => index }, "model_name" => "#{field}"}
+ if field.match(/\(\w+\)$/).blank?
+ model_name = field
+ else
+ model_name, field = field.scan(/\w+/)
+ end
+ column = { "#{field}_id" => { "columns" => { tmp => index }, "model_name" => model_name } }
+ end
+
+ unless field.match(/^\$\w+/).blank?
+ index += 1
+ symbol , field = field, field.delete("$")
+ if field.match(/\(\w+\)$/).blank?
+ model_name = field
+ else
+ model_name, field = field.scan(/\w+/)
+ end
+
+ field_columns = { }
+ while xls.cell((xls.first_row + 1), index) != symbol
+ raise "格式不匹配" if(index == xls.last_column)
+ field_columns[xls.cell((xls.first_row + 1), index)] = index
+ index += 1
+ end
+ column = { "#{field}_id" => { "columns" => field_columns, "model_name" => model_name } }
+ end
+
+ columns.merge!(column)
+ index += 1
+ end
+
+ return columns
+ end
+
+ def create model_name, xls_path
+ xls = find_excel(xls_path)
+ model_columns = divide(xls)
+
+ 3.upto(xls.last_row) do |row_index|
+ row = {}
+ model_columns.each do |column_name, column_index|
+ unless column_index.class == Hash
+ row[column_name] = xls.cell(row_index, column_index)
+ else
+ row[column_name] = self.foreign_key(xls, row_index, column_index)
+ end
+ end
+ model_name.split("_").collect(&:capitalize).join.constantize.create(row)
+ end
+ end
+end
+
+class MasterImporter
+ extend MasterImporterModule
+end
@@ -312,7 +312,7 @@ Pf.classes.commentStudent.MainPanel = Ext.extend(Ext.Panel, {
form.reset();
form.loadRecord(record);
//加载头像
- //$("#image img").attr("src", record.get('image/url'));
+ $("#image img").attr("src", record.get('image/url'));
currentStu = record;
scope.addComment(currentStu.get('comment'), true);
@@ -79,6 +79,8 @@ Pf.classes.homeIndex.MainPanel = Ext.extend(Ext.Panel, {
border: false,
containerScroll: true,
loadMask : true,
+ frame : true,
+ collapseMode: 'mini',
width: 250,
region: 'west',
split: true,
@@ -107,7 +109,7 @@ Pf.classes.homeIndex.MainPanel = Ext.extend(Ext.Panel, {
form.reset();
form.loadRecord(record);
//加载头像
- //$("#image img").attr("src", record.get('image/url'));
+ $("#image img").attr("src", record.get('image/url'));
}
}
}),
@@ -212,9 +214,6 @@ Pf.classes.homeIndex.MainPanel = Ext.extend(Ext.Panel, {
}, {
fieldLabel: '班级',
name: 'classes/name',
- }, {
- fieldLabel: '评级',
- name: 'grade',
}]
},{
defaults : { anchor : '95%' ,readOnly : true},
@@ -231,7 +230,7 @@ Pf.classes.homeIndex.MainPanel = Ext.extend(Ext.Panel, {
xtype : 'textarea',
fieldLabel: '备注',
name: 'remark',
- height : 80
+ //height : 80
}]
}]
},new Ext.form.FieldSet({
@@ -57,6 +57,12 @@ Pf.classes.student = Ext.extend(Ext.grid.EditorGridPanel,{
var _this = this;
var tbar = [
{
+ iconCls: "add",
+ text: "添加",
+ handler: function(){ _this.addSingleStudent().show() }
+ },
+ new Pf.util.ImportXlsBtn({ actionName : 'student', store : _this.store }),
+ {
iconCls: "delete",
text: "删除",
handler: function(){
Oops, something went wrong.

0 comments on commit 8f7d003

Please sign in to comment.