1. 程式人生 > 其它 >PowerDesigner根據Excel匯入模型

PowerDesigner根據Excel匯入模型

1.首先通過PowerDesigner建立物理模型;

2.Excel的結構如下:
第一行是表資訊,依次是:表編碼、表名稱、表註釋
第二行及後面為列資訊,依次是:列編碼、列名稱、列資料型別、是否必填(M必填、O選填)、列註釋

Person 人員資訊 記錄人員相關資訊
PersonId 人員編碼 Variable multibyte (40) M 人員編碼
PersonName 姓名 Variable multibyte (10) M 姓名
Resume 簡歷 Variable multibyte (255) O 簡歷
Remark 備註 Variable multibyte (255) O 備註

3.執行指令碼,彈出 “生成成功”後即建立完成;

' 第一行是表資訊,依次是:表編碼、表名稱、表註釋
' 第二行及後面為列資訊,依次是:列編碼、列名稱、列資料型別、是否必填(M必填、O選填)、列註釋
' Excel的sheet名稱統一為sheet1
'開始
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:\test\import.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

on error Resume Next

set table = mdl.Tables.CreateNew '建立一個 表實體

For rwIndex = 1 To 1000 '
    With x1.Workbooks(1).Worksheets("Sheet1")
        If .Cells(rwIndex, 1).Value = "" Then
        Exit For
        End If
                
        If rwIndex = 1 Then
            ' 表賦值
            table.Code=.Cells(rwIndex, 1).Value
            table.Name=.Cells(rwIndex, 2).Value
            table.Comment=.Cells(rwIndex, 3).Value
        Else        
            set col = table.Columns.CreateNew '建立一列/欄位            
            
            col.Code = .Cells(rwIndex, 1).Value
            col.Name = .Cells(rwIndex, 2).Value '指定列名
            col.DataType = .Cells(rwIndex, 3).Value '指定列資料型別
            col.Comment = .Cells(rwIndex, 5).Value '指定列說明
            
            If .Cells(rwIndex, 4).Value = "M" Then
                col.Mandatory = true '指定列是否可空 true 為不可空 
            End If
            
            If rwIndex = 2 Then
                'col.Primary = true '指定主鍵
            End If
        End If
    End With
Next
MsgBox "生成成功"

Exit Sub
End sub