将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