从Excel导入Powerdesigner的方式

今天用了powerDesigner的Excel导入,发现些问题,就此总结一下:

首先:必须要使用 ACCESS数据库的建立物理模型:

其次:使用快捷键ctrl+shift+x条用VBS命令脚本,(路径和表数量需要手动修改):

脚本如下:

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 "D:\work\Workspaces\PowerDesigner 15\Excel导入逆向工程(示例).xlsx" '指定 excel文档路径
   x1.Workbooks(1).Worksheets("Sheet1").Activate '指定要打开的sheet名称
Else
   HaveExcel = False
End If

a x1, mdl

sub a(x1, mdl)
dim rwIndex 
dim tableName
dim colname
dim table
dim col
dim count
Dim i

'on error Resume Next
For i=1 to 2 '指定要导入的sheet的个数
   set table = mdl.Tables.CreateNew '创建一个 表实体
   count = count+1
For rwIndex=1 To 200 step 1 '指定遍历的Excel行标
With x1.Workbooks(1).Worksheets(i) '定义需要写入的表结构所在的sheet
'MsgBox "生成数据 表结构共计 1" + CStr(.Cells(2.2).Value), vbOK + vbInformation, " 表"
   If .Cells(rwIndex, 1).Value = "" Then
      Exit For
   End If
   If .Cells(rwIndex, 3).Value = "" Then
      table.Name = .Cells(rwIndex,1).Value '指定 表名,如果在 Excel文档里有,也可以 .Cells(rwIndex, 3).Value 这样指定
      table.Code = .Cells(rwIndex,2).Value '指定 表名
      'tabale.DefaulValue = .Cells(rwIndex,6).value
   Else
      colName = .Cells(rwIndex,1).value
      set col = table.Columns.CreateNew '创建一列字段
      'MsgBox .Cells(rwIndex,1).Value,vbOK + vbInformation,"列"
      col.Name = .Cells(rwIndex,1).Value
      'MsgBox col.Name,vbOK + vbInformation,"列"
      col.Code = .Cells(rwIndex,2).Value '指定字段名
   
      col.Comment = .Cells(rwIndex,1).Value '指定列说明
      col.DataType = .Cells(rwIndex,3).Value '指定列数据类型
      'col.DefaultValue = .Cells(rwIndex,6).Value '指定列数据类型
      '设置主键
      If.Cells(rwIndex,4).Value = "Primary Key" Then
      col.Primary=true
      End If
      '设置空值
      If.Cells(rwIndex,4).Value <> "Primary Key" Then
	If.Cells(rwIndex,5).Value="NOT NULL"Then
      		col.Mandatory=true
	End If
      End If
   End If
End With

Next
Next
MsgBox "生成数据 表结构共计 " + CStr(count), vbOK + vbInformation, " 表"

Exit Sub
End sub

导入的表格格式如:

中文表名 baseTable5      
name code type PKEY IS NULL
a阿斯蒂芬 a int(10) Primary Key NOT NULL
阿斯蒂芬 b varchar(10)    

 不过注意在EXCEL是有合并单元格的情况的要注意,是读取出错的,

另外,Mandatory的属性设置是跟primary有冲突

所以要避免两个一起设置

所以才有这段判断:

      '设置空值
      If.Cells(rwIndex,4).Value <> "Primary Key" Then
	If.Cells(rwIndex,5).Value="NOT NULL"Then
      		col.Mandatory=true
	End If
      End If

猜你喜欢

转载自lncdzh.iteye.com/blog/2261688