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

posted @ 2020-07-19 14:10  段江涛IT  阅读(1934)  评论(0编辑  收藏  举报
页脚HTML代码