当数据库表太多时,使用PowerDesigner手动建模,效率极其低下,这时候就可以使用脚本进行批量导入创建
1. Excel样式
- 第一行是表信息的描述,依次是:表名、表注释
- 第二行开始是字段的描述,分别是:字段名、字段注释、数据类型、否为空(NO不为空)、主键(KEY)
- 每一个Sheet都是一个数据表
2. 新建物理模型
打开PowerDesigner软件,新建物理模型以及选择自己使用的数据库类型,如下图:
3. 脚本
' 第一行是表信息的描述,依次是:表名、表注释
' 第二行开始是字段的描述,分别是:字段名、字段注释、数据类型、否为空(NO不为空)、主键(KEY)
' 开始
Option Explicit
Dim mdl ' the current model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim HaveExcel
Dim RQ
RQ = vbYes ' MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")
If RQ = vbYes Then
HaveExcel = True
' Open & Create Excel Document
Dim x1 '
Set x1 = CreateObject("Excel.Application")
x1.Workbooks.Open "C:\Users\Administrator\Desktop\test.xlsx" '指定Excel文档路径
Else
HaveExcel = False
End If
' 定义相关变量
a x1, mdl
sub a(x1, mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim sheet
' 循环所有sheet
For Each sheet In x1.Worksheets
' 逐步遍历行数
For rwIndex = 1 To 1000 '
With sheet
' 退出
If .Cells(rwIndex, 1).Value = "" Then
Exit For
End If
If rwIndex = 1 Then
set table = mdl.Tables.CreateNew ' 创建一个表实体
table.Name = .Cells(rwIndex, 2).Value ' 表名
table.Code = .Cells(rwIndex, 1).Value ' 表标识
table.Comment = .Cells(rwIndex, 2).Value ' 表注释
Else
set col = table.Columns.CreateNew ' 创建一列/字段
col.Name = .Cells(rwIndex, 2).Value ' 列名
col.Code = .Cells(rwIndex, 1).Value ' 列标识
col.DataType = .Cells(rwIndex, 3).Value ' 数据类型
col.Comment = .Cells(rwIndex, 2).Value ' 列注释
If .Cells(rwIndex, 4).Value = "NO" Then
col.Mandatory = true ' 指定列是否可空, true为不可空
End If
If .Cells(rwIndex, 5).Value = "KEY" Then
col.Primary = true ' 指定列是否主键, true为主键
End If
End If
End With
Next
Next
MsgBox "生成成功"
Exit Sub
End sub
4. 执行脚本
模型建立完成后,使用快捷键crtl + shift + x打开执行脚本窗口,将替换Excel路径后的脚本复制到窗口内,点击Run开始进行表结构的转换,如下图:
5. 查看结果
如果没有显示生成的表,可以右键PhysicalDiagram,选择show symbols,点击全选,点击确定即可看到生成的表,如下图: