我們經常會在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:\XXX\XXXX.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模板如下: