PowerDesigner從Excel導入表


PowerDesigner要導入Excel,需要使用到VB語法,同時PowerDesigner集成了訪問Excel的方法,VB代碼如下:

' 第一行是表信息的描述,依次是:表名、表Code、表注釋
' 第二行開始是列的描述,分別是:列名、列Code、列數據類型、列注釋
' 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 "E:\table.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 = "" 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

Excel的表結構如下:

 

 

  最后說明:

1. 第一行是表信息的描述,依次是:表名、表Code、表注釋
2. 第二行開始是列的描述,分別是:列名、列Code、列數據類型、是否為空、列注釋
3.Excel的sheet名稱統一為sheet1

4.Excel的位置是:E:\table.xlsx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM