PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型
本文使用的数据库类型是Oracle 11g
最近在工作中遇到一个问题:数据的设计以表格的形式保存在Excel文件中。(由于保密原因,我只能看到数据库设计文档,无法访问数据库。=_=!)
其中包括Name,Code,DataType,Unit,Length,Precision,Primary,Foreign Key,Mandatory,Comment等字段。
现在我要使用PowerDesigner重新建立这些表的物理模型,但是面对好几百个表,如果使用复制粘贴的方式不但费事费力,而且容易出错。
PowerDesigner提供了很多扩展功能,其中包括从Excel文件导入物理数据模型。
导入的方式有两种,一种是把表的设计模型整理为PowerDesigner要求的格式,另外一种是编写VBScript脚本。
由于编写VBScritp比较灵活,而且把表的设计整理为PowerDesigner需要的格式也比较麻烦,所以通过编写VBScript脚本导入Excel中的模型数据。
1 '============================================================ 2 '从Excel文件中导入PowerDesigner 物理数据模型 3 ' 4 '注意:1,Excel表格中不能有合并的单元格 5 ' 2,列之间不能有空行 6 '============================================================ 7 8 9 Option Explicit 10 11 '============================================================ 12 '私有全局变量。 13 '============================================================ 14 Private CURRENT_MODEL_NAME 15 Private CURRENT_MODEL 16 Private TABLES 17 Private EXCEL_APP 18 Private FILE_PATH 19 20 CURRENT_MODEL_NAME = "Excel导入" 21 Set EXCEL_APP = CreateObject("Excel.Application") 22 FILE_PATH="D:\models.xlsx" '文件的绝对路径 23 24 '检查文件是否存在 25 If CheckFileExsistence() Then 26 '检查当前是否有已经打开的物理图 27 Call GetModelByName(CURRENT_MODEL) 28 If CURRENT_MODEL Is Nothing Then 29 MsgBox("请先打开一个名称为“" & CURRENT_MODEL_NAME & "”的物理数据模型(Physical Data Model),然后再进执行导入!") 30 Else 31 Set TABLES = CURRENT_MODEL.Tables 32 '根据EXCEL表格创建模型 33 ImportModels() 34 End If 35 Else 36 MsgBox "文件" + FILE_PATH + "不存在!" 37 End If 38 39 40 '============================================================ 41 '导入模型 42 '============================================================ 43 Sub ImportModels 44 '打开Excel文件 45 Dim Filename 46 Dim ReadOnly 47 EXCEL_APP.Workbooks.Open FILE_PATH 48 49 Dim worksheets 50 Dim worksheetCount 51 Set worksheets = EXCEL_APP.Worksheets 52 worksheetCount = worksheets.Count 53 If worksheetCount <= 0 Then 54 Exit Sub 55 End If 56 57 Dim index 58 Dim currentSheet 59 For index = 1 to worksheetCount 60 Set currentSheet = worksheets(index) 61 Call CreateTable(currentSheet) 62 Next 63 64 '关闭Excel文件 65 EXCEL_APP.Workbooks.Close 66 End Sub 67 68 69 '============================================================ 70 '创建表 71 '============================================================ 72 Sub CreateTable(ByRef worksheet) 73 Dim cells 74 Set cells = worksheet.Cells 75 Dim table 76 77 '检查具有相同名称的表是否已经存在 78 Call GetTableByName(table, worksheet.Name) 79 If table Is Nothing Then 80 Set table = TABLES.CreateNew 81 Set table = TABLES.CreateNew 82 table.Name = cells(1, 1).Value 83 table.Code = cells(2, 1).Value 84 table.Comment = cells(3, 1).Value 85 End If 86 87 Dim index 88 Dim rows 89 Dim col 90 Set rows = worksheet.Rows 91 For index = 4 to 512 92 If EXCEL_APP.WorksheetFunction.CountA(rows(index)) <= 0 Then 93 Exit For 94 End If 95 96 '判断列是否已经存在 97 If Not ColumnExists(table, cells(index, 1).Value) Then 98 Set col = table.Columns.CreateNew 99 col.Name = cells(index, 1).Value '字段的中文含义 100 col.Code = cells(index, 2).Value '字段名 101 col.Unit = cells(index, 4).Value '字段的单位 102 col.DataType = cells(index, 3).Value '字段的数据类型 103 'col.DataType = GenerateDataType(cells(index, 3).Value, cells(index, 4).Value, cells(index, 5).Value) '字段的数据类型 104 col.Comment = cells(index, 5).Value '字段的注释 105 End If 106 Next 107 End Sub 108 109 110 '============================================================ 111 '检查文件是否存在 112 '============================================================ 113 Function CheckFileExsistence 114 Dim fso 115 Set fso = CreateObject("Scripting.FileSystemObject") 116 CheckFileExsistence = fso.FileExists(FILE_PATH) 117 End Function 118 119 120 '============================================================ 121 '根据数据类型名称,精度和刻度生成数据类型 122 '============================================================ 123 Function GenerateDataType(dataTypeName, precision, scale) 124 Select Case Ucase(dataTypeName) 125 Case Empty 126 GenerateDataType = Empty 127 Case "NUMBER" 128 GenerateDataType = "NUMBER(" & precision & "," & scale & ")" 129 End Select 130 End Function 131 132 133 '============================================================ 134 '获取指定指定名称的数据模型 135 '============================================================ 136 Sub GetModelByName(ByRef model) 137 Dim md 138 For Each md in Models 139 If StrComp(md.Name, CURRENT_MODEL_NAME) = 0 Then 140 Set model = md 141 Exit Sub 142 End If 143 Next 144 Set model = Nothing 145 End Sub 146 147 148 '============================================================ 149 '根据表名称获取对应的表 150 '============================================================ 151 Sub GetTableByName(ByRef table, tableName) 152 Dim tb 153 For Each tb in TABLES 154 If StrComp(tb.Name, tableName) = 0 Then 155 Set table = tb 156 Exit Sub 157 End If 158 Next 159 Set table = Nothing 160 End Sub 161 162 163 '============================================================ 164 '检查字段是否已经存在 165 '============================================================ 166 Function ColumnExists(ByRef table, columnName) 167 Dim col 168 For Each col in table.Columns 169 If StrComp(col.Name, columnName) = 0 Then 170 ColumnExists = True 171 Exit Function 172 End If 173 Next 174 ColumnExists = False 175 End Function
下面是测试脚本使用的Excel文件格式:
前三行的第一个单元格中的值分别是:表名称,数据库表名称,表的说明信息。
第4行到低7行都是列信息。
A到H列依次为:列名,字段名称,数据类型,单位,是否主键,是否外键,是否非空,列注释信息。
以下是Excel导入的步骤:
1,在PowerDesigner中先打开一个物理数据模型,然后按Ctrl + Shift + X执行脚本。
执行完成之后的界面如下:
导入完成之后PhysicalDiagram_1中并没有显示表的符号。
在PowerDesigner的元数据模型API文档中没有找到对应的API所以就这样将就了;D
2,展开上图所示的Tables文件夹。左键点击图标,把表拖到指定的物理图(Physical Diagram)中。
这样就实现了从Excel中导入数据库物理模型。
总结:
这个脚本虽然实现了导入功能,但是还不够完善。第二步要手动完成,因为我没有找到相应的API =_=!。
此外,脚本中没有设置相应的主键,外键和非空约束等。
如果哪位知道如何使用脚本实现第二步,请告诉我!谢谢!