zoukankan      html  css  js  c++  java
  • 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:UsershuageDesktop	est11.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:UsershuageDesktop	est11.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:UsershuageDesktop est11.xlsx注意这个路径要与脚本中的路径一致

    5.2运行脚本
    5.3检查导入效果

     

  • 相关阅读:
    pychram 2018-01 安装pyQT5报错
    pyqt 8行内就可以跑一个浏览器
    sql server无log ldf日志文件附件mdf数据库重新生成ldf日志文件
    解决Specifying a namespace in include()withou providing an app_name
    Java 连接池的工作原理(转)
    使用from __future__ import unicode_literals时要注意的问题
    详细介绍Redis的几种数据结构以及使用注意事项(转)
    再谈Redis应用场景(转)
    Redis的LRU机制(转)
    深入理解Redis主键失效原理及实现机制(转)
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5542078.html
Copyright © 2011-2022 走看看