Excel文件样例
样例数据如下图所示
在powerdesigner中打开工具
在powerdesigner中点击工具—>Execute Commands —>Edit/Run Script,或者使用快捷键ctrl+shift+x打开脚本窗口
执行代码
注意将下方代码文件路径、sheet页名、rwIndex进行校对
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 "C:\Users\229\Desktop\test.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
dim count
on error Resume Next
set table = mdl.Tables.CreateNew '创建一个表实体
table.Name = .Cells(rwIndex,1).Value '指定表名,如果在Excel文档里有,也可以 .Cells(rwIndex, 3).Value 这样指定
table.Code = .Cells(rwIndex, 2).Value '指定表名
count = count + 1
ru
For rwIndex = 2 To 1000 '指定要遍历的Excel行标 由于第1行是表头,从第2行开始
With x1.Workbooks(1).Worksheets("Sheet1")
If .Cells(rwIndex, 1).Value = "" Then
Exit For
End If
if table.Name = .Cells(rwIndex,1).Value then
set col = table.Columns.CreateNew '创建一列/字段
'If .Cells(rwIndex, 5).Value = "" Then
' col.Name = "NULL" '指定列名
'Else
col.Name = .Cells(rwIndex, 5).Value
'End If
col.Code = .Cells(rwIndex, 3).Value '指定列名
col.DataType = .Cells(rwIndex, 4).Value '指定列数据类型
If .Cells(rwIndex, 10).Value = "N" Then
col.Mandatory = true '指定列是否可空 true 为不可空
End If
If .Cells(rwIndex, 6).Value = "Y" Then
col.Primary = true '指定主键
End If
col.Precision=.Cells(rwIndex, 8).Value '指定长度
col.Scale=.Cells(rwIndex, 9).Value '指定精度
col.Length=.Cells(rwIndex, 7).Value '指定字节
col.Isidentity=.Cells(rwIndex, 12).Value '指定标识
else
set table = mdl.Tables.CreateNew '创建一个表实体
table.Name = .Cells(rwIndex,1).Value '指定表名,如果在Excel文档里有,也可以 .Cells(rwIndex, 3).Value 这样指定
table.Code = .Cells(rwIndex, 2).Value '指定表名
count = count + 1
ru
set col = table.Columns.CreateNew '创建一列/字段
'If .Cells(rwIndex, 5).Value = "" Then
' col.Name = "NULL" '指定列名
'Else
col.Name = .Cells(rwIndex, 5).Value
'End If
col.Code = .Cells(rwIndex, 3).Value '指定列名
col.DataType = .Cells(rwIndex, 4).Value '指定列数据类型
If .Cells(rwIndex, 10).Value = "N" Then
col.Mandatory = true '指定列是否可空 true 为不可空
End If
If .Cells(rwIndex, 6).Value = "Y" Then
col.Primary = true '指定主键
End If
col.Precision=.Cells(rwIndex, 8).Value '指定长度
col.Scale=.Cells(rwIndex, 9).Value '指定精度
col.Length=.Cells(rwIndex, 7).Value '指定字节
col.Isidentity=.Cells(rwIndex, 12).Value '指定标识
End If
End With
Next
MsgBox "生成数据表结构共计 " + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub
执行结果
table_1 是无用的,删掉即可
参考文章
单表导入https://blog.csdn.net/weixin_44792849/article/details/115554371
end
边查边写的vba代码,写的比较粗糙,欢迎优化