PowerDesigner16通过导入excel生成数据表
一、准备工作
- 电脑安装PowerDesigner16;
- 准备好要导入的excel;
如下图所示:
【注:第二行的列名可根据自身需求定!】
二、PowerDesigner16工作空间
打开PowerDesigner16软件,在工作空间中:
01、点击:菜单栏文件——>新建模型——>Model types——>Physical Data Model; (如下图)
02. 脚本(可根据你的excel列名修改脚本)
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\2019\***.xls"
x1.Workbooks(1).Worksheets("Sheet1").Activate
Else
HaveExcel = False
End If
a x1, mdl
sub a(x1,mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim count
'可根据字段多少修改值(200)的大小
For rwIndex = 1 To 200 step 1
With x1.Workbooks(1).Worksheets("Sheet1")
'MsgBox "生成数据表结构共计1 ="+CStr(.Cells(2,2).Value ), vbOK + vbInformation, "表"
'当值为空自加一行rwIndex + 1
If .Cells(rwIndex, 1).Value = "" Then
rwIndex = rwIndex + 1
IF .Cells(rwIndex, 1).Value = "" then
Exit For
end if
End If
'当第四列为空时,为表table的name、code、comment赋值,观察我们的excel表格,不难发现正是第一行,占三格。
If .Cells(rwIndex, 4).Value = "" Then
set table = mdl.Tables.CreateNew
table.Name = .Cells(rwIndex , 1).Value
table.Code = .Cells(rwIndex , 2).Value
table.Comment = .Cells(rwIndex , 3).Value
rwIndex = rwIndex + 1
count = count + 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,3).Value
'数据类型
col.DataType = .Cells(rwIndex, 4).Value
End If
End With
Next
MsgBox "生成数据表结构共计" + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub
03、注意:
- 须修改脚本中
x1.Workbooks.Open "C:\Users\2019\***.xls"
在双引号中修改你的excel文件的绝对地址;
- 如果你的列顺序与文中图示中的列顺序不一致,请修改
'字段中文名
col.Name = .Cells(rwIndex, 1).Value
'字段英文名
col.Code = .Cells(rwIndex, 2).Value
'注解
col.Comment = .Cells(rwIndex,3).Value
'数据类型
col.DataType = .Cells(rwIndex, 4).Value
- 若你的表中不止四列,有其他属性,则无需删除!
- PowerDesigner16可根据数据类型自动在生成的table中添加字段长度Length以及小数位Precision,无需在excel及脚本中写入。
三、生成table
如果你完成了上面的步骤,接下来我们就可以生成table啦。
1、按住Ctrl+Shift+X进入脚本运行界面。(如下图)
2、确认脚本无误后点击Run运行即可。
四、总结与避坑
1、看多篇不如仔细琢磨一篇!
2、
2、运行脚本前务必检查一下你的excel表中的中文字段名和英文字段名是否有相同项,如有相同项请区分一下,否则运行时会报错!
一碗牛大加俩蛋@你了、有收获?希望老铁们来个三连击,激励一下作者,也希望能让更多的同学看到这篇文章!!!
最后顺便推荐几本 Java 技术栈的书籍,建议从左到右的顺序看
Java:《Java核心技术卷1》、《编程思想》、《深入理解Java虚拟机》
Linux:《鸟哥的Linux私房菜》