powerdesigner-从excel导入table模型
Posted y酒味i
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powerdesigner-从excel导入table模型相关的知识,希望对你有一定的参考价值。
近在使用pd过程中,遇到一个问题,就是类的字段,方法,类型在excel中整理好了,想导入到pd直接生成类图。网上有很多生成实体表的方法,于是自己模仿写了一个生成类图的,在pd中的工具--扩展--脚本,或者直接快捷键shift + ctrl + X 打开脚本窗口,执行以下代码即可
1.编写EXCEL:
2.打开PowerDesigner,创建物理模型(Physical Data Model)-因不同的pd模型在使用时 是不通的编码-所以这里测试使用Physical Data Model
3.在PowerDesigner菜单栏中,依次点击“Tools ->Excute Commands->Edit/Run Script..”
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\\huage\\Desktop\\test\\11.xlsx" 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 \'on error Resume Next For rwIndex = 1 To 1000 step 1 With x1.Workbooks(1).Worksheets("Sheet1") \'MsgBox "生成数据表结构共计1 ="+CStr(.Cells(2,2).Value ), vbOK + vbInformation, "表" If .Cells(rwIndex, 1).Value = "" Then Exit For End If If .Cells(rwIndex, 3).Value = "" Then set table = mdl.Tables.CreateNew table.Name = .Cells(rwIndex , 1).Value table.Code = .Cells(rwIndex , 2).Value count = count + 1 Else colName = .Cells(rwIndex, 1).Value set col = table.Columns.CreateNew \'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列" col.Name = .Cells(rwIndex, 1).Value \'MsgBox col.Name, vbOK + vbInformation, "列" col.Code = .Cells(rwIndex, 2).Value col.Comment = .Cells(rwIndex,1).Value col.DataType = .Cells(rwIndex, 3).Value End If End With Next MsgBox "生成数据表结构共计" + CStr(count), vbOK + vbInformation, "表" Exit Sub End sub
第二种-有解析版(但有写小bug)
\'开始 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\\huage\\Desktop\\test\\11.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 = "Sheet1" \'指定 表名,如果在 Excel文档里有,也可以 .Cells(rwIndex, 3).Value 这样指定 table.Code = "Sheet1" \'指定 表名 count = count + 1 For rwIndex = 1 To 1000 \'指定要遍历的 Excel行标 由于第1行是 表头, 从第2行开始 With x1.Workbooks(1).Worksheets("Sheet1") If .Cells(rwIndex, 1).Value = "" Then Exit For End If set col = table.Columns.CreateNew \'创建一列/字段 \'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列" If .Cells(rwIndex, 3).Value = "" Then col.Name = .Cells(rwIndex, 1).Value \'指定列名 Else col.Name = .Cells(rwIndex, 3).Value End If \'MsgBox col.Name, vbOK + vbInformation, "列" col.Code = .Cells(rwIndex, 1).Value \'指定列名 col.DataType = .Cells(rwIndex, 2).Value \'指定列数据类型 col.Comment = .Cells(rwIndex, 5).Value \'指定列说明 If .Cells(rwIndex, 4).Value = "否" Then col.Mandatory = true \'指定列是否可空 true 为不可空 End If If rwIndex = 2 Then col.Primary = true \'指定主键 End If End With Next MsgBox "生成数据 表结构共计 " + CStr(count), vbOK + vbInformation, " 表" Exit Sub End sub
5.测试
5.1用的EXCEL:C:\\Users\\huage\\Desktop\\test\\11.xlsx注意这个路径要与脚本中的路径一致
5.2运行脚本
5.3检查导入效果
以上是关于powerdesigner-从excel导入table模型的主要内容,如果未能解决你的问题,请参考以下文章