我们经常会在excel中设计整理表结构,整理完需要导入到Powerdesigner中,可以通过以下脚本来实现快速,具体操作方法:
打开PowerDesigner,新建模型,点击Tools|Execute Commands|Edit/Run Script菜单或按下快捷键Ctrl + Shift + X打开脚本窗口,输入示例VBScript脚本,修改其中的Excel模板路径,点Run按钮执行即可
1 Option Explicit 2 Dim mdl ' the current model 3 Set mdl = ActiveModel 4 If (mdl Is Nothing) Then 5 MsgBox "There is no Active Model" 6 End If 7 Dim HaveExcel 8 Dim RQ 9 RQ = vbYes 'MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation") 10 If RQ = vbYes Then 11 HaveExcel = True 12 ' Open & Create Excel Document 13 Dim x1 ' 14 Set x1 = CreateObject("Excel.Application") 15 x1.Workbooks.Open "D:XXXXXXX.xls" '指定excel文档路径 16 Else 17 HaveExcel = False 18 End If 19 a x1, mdl 20 sub a(x1,mdl) 21 dim rwIndex 22 dim tableName 23 dim colname 24 dim table 25 dim col 26 dim count 27 28 'on error Resume Next 29 30 dim shtIdx 31 for shtIdx=2 to x1.Workbooks(1).Worksheets.Count '第二个sheet页开始 32 For rwIndex = 1 To 120 step 1 33 With x1.Workbooks(1).Worksheets(shtIdx) 34 'With x1.Workbooks(1).Worksheets(tName) 35 'MsgBox "生成数据表结构共计1 ="+CStr(.Cells(2,2).Value ), vbOK + vbInformation, "表" 36 If .Cells(rwIndex, 1).Value = "" Then 37 Exit For 38 End If 39 if rwIndex=1 Then 40 '创建一个表实体 41 set table = mdl.Tables.CreateNew 42 '指定表名 43 table.Name = .Cells(2 , 2).Value 44 '指定表Code 45 table.Code = .Cells(2 , 1).Value 46 'Comment 47 table.Comment = .Cells(2 , 2).Value 48 count = count + 1 49 End If 50 if rwIndex>1 Then '忽略表头 51 colName = .Cells(rwIndex, 1).Value 52 set col = table.Columns.CreateNew '创建列 53 'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列" 54 col.Name = .Cells(rwIndex, 3).Value '指定列名 55 'MsgBox col.Name, vbOK + vbInformation, "列" 56 col.Code = .Cells(rwIndex, 3).Value '指定列code 57 col.DataType = .Cells(rwIndex, 4).Value '指定列数据类型 58 col.Comment = .Cells(rwIndex,5).Value '指定列Comment 59 If .Cells(rwIndex, 6).Value = "Y" Then'指定主键 60 col.Primary = true 61 End If 62 '指定列是否可空 63 'If .Cells(rwIndex, 7).Value = "Y" Then 64 ' col.Mandatory = true'指定列是否可空,true为不可空 65 'End If 66 End If 67 End With 68 Next 69 Next 70 MsgBox "生成数据表结构共计" + CStr(count), vbOK + vbInformation, "表" 71 Exit Sub 72 End sub
Excel模板如下: