5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/modle_to_excel.rb', line 5
def self.to_excel(file_name)
Spreadsheet.client_encoding = "UTF-8"
book = Spreadsheet::Workbook.new
first_row = ["编号","字段","类型","注释"]
sheet = book.create_worksheet :name => "表注释"
sheet.row(0).concat ["编号","表名","中文名","注释"]
num = 1
ActiveRecord::Base.connection.tables.each do |table|
next if table.match(/\Aschema_migrations\z/)
begin
class_name = table.singularize.classify
columns = class_name.constantize.columns
sheet[num, 0] = num
sheet[num, 1] = table.to_s
sheet[num, 2] = ""
sheet[num, 3] = ""
num += 1
rescue Exception => e
puts "table"
puts table
next
end
end
ActiveRecord::Base.connection.tables.each do |table|
next if table.match(/\Aschema_migrations\z/)
begin
class_name = table.singularize.classify
columns = class_name.constantize.columns
sheet = book.create_worksheet :name => table
sheet.row(0).concat first_row
num = 1
columns.each do |column|
puts column
puts column.type
sheet[num, 0] = num
sheet[num, 1] = column.name
sheet[num, 2] = column.type.to_s
sheet[num, 3] = ""
num += 1
end
rescue Exception => e
puts "table"
puts table
next
end
end
book.write "#{Rails.root}/public/#{file_name}.xls"
end
|