將Excel的表批量導入到powerdesigner中

將Excel的表批量導入到powerdesigner中

  1. 創建Excel中的sheet按照以下格式
中文表名 6G掃頻數據
英文表名 sweep_frequency_6g
中文字段 英文字段 字段類型 註釋
時間 Time VARCHAR(255) 時間
緯度 Latitude VARCHAR(256) 緯度
經度 Longitude VARCHAR(257) 經度
小區ID Cell ID VARCHAR(258) 小區ID
  1. 打開powerdesigner按快捷鍵Ctrl+Shift+X 打開腳本執行界面
  2. 替換下方代碼中的excel的路徑點擊Run即可導入
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 '
 Dim wb
 Dim Sht
 Dim count
 Dim newTableName  'sheet中的表名
 Dim singleTable   'powerdesigner中的表名
 Dim existsFlag
  Set x1 = CreateObject("Excel.Application")
  '打開excel文件
  Set wb = x1.Workbooks.Open("C:\Users\Administrator\Desktop\test2.xlsx")
    '遍歷每個sheet
  For Each Sht In wb.Sheets
        '打印sheet名
         'msgbox( Sht.Name)
         'msgbox( mdl.Tables.count)
         '獲取到當前需要創建的表名,查看它是否在powerdesigner中已經存在
         newTableName = Sht.Cells(1, 2).Value
         'msgbox( newTableName)
         For Each singleTable In mdl.Tables
         
            'msgbox(singleTable.Name)
             If singleTable.Name = newTableName Then
               existsFlag = True
                End If
         Next
      If existsFlag Then
         MsgBox (newTableName + "已經存在")
         '重置flag
         existsFlag = False
      Else
      
         '調用子模塊
         immigrate_function Sht, mdl
         count = count + 1
      End If
   Next
  MsgBox "生成數據表結構共計" + CStr(count), vbOK + vbInformation, "表"
  
'關閉流
Set Sht = Nothing
wb.Close
Set wb = Nothing
x1.Quit
Set x1 = Nothing

Else
   HaveExcel = False
End If
 '子程序模塊
Sub immigrate_function(Sht, mdl)
Dim rwIndex
Dim tableName
Dim colname
Dim table
Dim col
'on error Resume Next
For rwIndex = 1 To 1000 Step 1
With Sht
   If .Cells(rwIndex, 1).Value = "" Then
       rwIndex = rwIndex + 1
       If .Cells(rwIndex, 1).Value = "" Then
       Exit For
       End If
   End If
  If rwIndex = 1 Then
    Set table = mdl.Tables.CreateNew
        table.Name = .Cells(rwIndex, 2).Value
           ElseIf rwIndex = 2 Then
        table.Code = .Cells(rwIndex, 2).Value
       rwIndex = rwIndex + 1
      Else
    colname = .Cells(rwIndex, 1).Value
    Set col = table.Columns.CreateNew
    col.Name = .Cells(rwIndex, 1).Value
    col.Code = .Cells(rwIndex, 2).Value
    col.Comment = .Cells(rwIndex, 4).Value
    col.DataType = .Cells(rwIndex, 3).Value
   End If
  End With
  Next
 End Sub
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章