powerdesigner-从excel导入table模型
生活随笔
收集整理的這篇文章主要介紹了
powerdesigner-从excel导入table模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
近在使用pd過程中,遇到一個問題,就是類的字段,方法,類型在excel中整理好了,想導入到pd直接生成類圖。網上有很多生成實體表的方法,于是自己模仿寫了一個生成類圖的,在pd中的工具--擴展--腳本,或者直接快捷鍵shift + ctrl + X打開腳本窗口,執行以下代碼即可
1.編寫EXCEL:
2.打開PowerDesigner,創建物理模型(Physical Data Model)-因不同的pd模型在使用時 是不通的編碼-所以這里測試使用Physical Data Model
3.在PowerDesigner菜單欄中,依次點擊“Tools ->Excute Commands->Edit/Run Script..”
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:UsershuageDesktop est11.xlsx"
x1.Workbooks(1).Worksheets("Sheet1").Activate
Else
HaveExcel = False
End If
a x1, mdl
sub a(x1,mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim count
'on error Resume Next
For rwIndex = 1 To 1000 step 1
With x1.Workbooks(1).Worksheets("Sheet1")
'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
set table = mdl.Tables.CreateNew
table.Name = .Cells(rwIndex , 1).Value
table.Code = .Cells(rwIndex , 2).Value
count = count + 1
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
End If
End With
Next
MsgBox "生成數據表結構共計" + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub
第二種-有解析版(但有寫小bug)
'開始
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:UsershuageDesktop est11.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
on error Resume Next
set table = mdl.Tables.CreateNew '創建一個 表實體
table.Name = "Sheet1" '指定 表名,如果在 Excel文檔里有,也可以 .Cells(rwIndex, 3).Value 這樣指定
table.Code = "Sheet1" '指定 表名
count = count + 1
For rwIndex = 1 To 1000 '指定要遍歷的 Excel行標 由于第1行是 表頭, 從第2行開始
With x1.Workbooks(1).Worksheets("Sheet1")
If .Cells(rwIndex, 1).Value = "" Then
Exit For
End If
set col = table.Columns.CreateNew '創建一列/字段
'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列"
If .Cells(rwIndex, 3).Value = "" Then
col.Name = .Cells(rwIndex, 1).Value '指定列名
Else
col.Name = .Cells(rwIndex, 3).Value
End If
'MsgBox col.Name, vbOK + vbInformation, "列"
col.Code = .Cells(rwIndex, 1).Value '指定列名
col.DataType = .Cells(rwIndex, 2).Value '指定列數據類型
col.Comment = .Cells(rwIndex, 5).Value '指定列說明
If .Cells(rwIndex, 4).Value = "否" Then
col.Mandatory = true '指定列是否可空 true 為不可空
End If
If rwIndex = 2 Then
col.Primary = true '指定主鍵
End If
End With
Next
MsgBox "生成數據 表結構共計 " + CStr(count), vbOK + vbInformation, " 表"
Exit Sub
End sub
5.測試
5.1用的EXCEL:C:UsershuageDesktop est11.xlsx注意這個路徑要與腳本中的路徑一致
5.2運行腳本
5.3檢查導入效果
總結
以上是生活随笔為你收集整理的powerdesigner-从excel导入table模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server创建表超出行最大限制
- 下一篇: 学习笔记103—win10连接惠普打印机