1. 程式人生 > 實用技巧 >PowerDesigner從Excel匯入表

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.Name=.Cells(rwIndex, 1).Value table.Code=.Cells(rwIndex, 2).Value table.Comment=.Cells(rwIndex, 3).Value Else set col = table.Columns.CreateNew '建立一列/欄位 col.Name = .Cells(rwIndex, 1).Value col.Code = .Cells(rwIndex, 2).Value '指定列名 col.DataType = .Cells(rwIndex, 3).Value '指定列資料型別 col.Comment = .Cells(rwIndex, 4).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