將Excel的表批量導入到powerdesigner中
- 創建Excel中的sheet按照以下格式
中文表名 |
6G掃頻數據 |
|
|
英文表名 |
sweep_frequency_6g |
|
|
中文字段 |
英文字段 |
字段類型 |
註釋 |
時間 |
Time |
VARCHAR(255) |
時間 |
緯度 |
Latitude |
VARCHAR(256) |
緯度 |
經度 |
Longitude |
VARCHAR(257) |
經度 |
小區ID |
Cell ID |
VARCHAR(258) |
小區ID |
- 打開powerdesigner按快捷鍵Ctrl+Shift+X 打開腳本執行界面
- 替換下方代碼中的excel的路徑點擊Run即可導入
Option Explicit
Dim mdl
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim HaveExcel
Dim RQ
RQ = vbYes
If RQ = vbYes Then
HaveExcel = True
Dim x1
Dim wb
Dim Sht
Dim count
Dim newTableName
Dim singleTable
Dim existsFlag
Set x1 = CreateObject("Excel.Application")
Set wb = x1.Workbooks.Open("C:\Users\Administrator\Desktop\test2.xlsx")
For Each Sht In wb.Sheets
newTableName = Sht.Cells(1, 2).Value
For Each singleTable In mdl.Tables
If singleTable.Name = newTableName Then
existsFlag = True
End If
Next
If existsFlag Then
MsgBox (newTableName + "已經存在")
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
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