excel表设计模型转powerdesigner pdm模型

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/cblstc/article/details/102585777

文章目录

前言

最近使用excel设计表结构,想根据表结构在powerdesigner生成pdm模型,进而生成sql语句。一开始每个表都去复制粘贴,由于表数量太多,最终放弃,考虑使用脚本的方式直接转换。网上普遍使用vb脚本完成这个功能,由于vb我并没学过,所以在别人代码的基础上稍加修改,为自己所用。代码不可能完美,只为满足功能需要,如有错误,敬请谅解。

代码

Option Explicit

Dim excelPath,colNameIndex,colCommentIndex,colTypeIndex,colTypeLengthIndex,colIsPrimaryKeyIndex,colIsNotNullIndex,colDefaultValueIndex,colMultiValueIndex

'......修改区域......
excelPath = "D:\develop\测试excel.xlsx" 'excel文件路径(路径根据实际情况)
colNameIndex = 1 '字段名下标(下标根据实际情况)
colCommentIndex = 2 '字段注释下标(下标根据实际情况)
colTypeIndex = 3 '字段类型下标(下标根据实际情况)
colTypeLengthIndex = 4 '字段类型长度下标(下标根据实际情况)
colIsPrimaryKeyIndex = 5 '是否主键下标(下标根据实际情况)
colIsNotNullIndex = 6 '是否唯一下标(下标根据实际情况)
colDefaultValueIndex = 7 '默认值下标(下标根据实际情况)
colMultiValueIndex = 8 '多值下标(下标根据实际情况)
'......修改区域......

Dim mdl,x1sApp,xlsWorkBook,xlsSheet

Set mdl = ActiveModel
If (mdl Is Nothing) Then
    MsgBox "There is no Active Model"
End If
'打开excel文档
Set x1sApp = CreateObject("Excel.Application")
Set xlsWorkBook = x1sApp.Workbooks.Open(excelPath)
Set xlsSheet = x1sApp.Workbooks(1).Worksheets("Sheet1")

a x1sApp,mdl,x1sApp,xlsWorkBook,xlsSheet
Sub a(x1, mdl,x1sApp,xlsWorkBook,xlsSheet)
    
    Dim rowNum,table,col,count,rowCount
    rowCount = xlsSheet.usedRange.Rows.Count
    
    On Error Resume Next
    
    '遍历每一行
    For rowNum = 1 To rowCount
        With xlsSheet
            If .Cells(rowNum, 1).Value <> "" And .Cells(rowNum, 1).Value <> "字段名" Then '第一列不为空
            If .Cells(rowNum, 3).Value = "" Then '如果遍历到第三列为空,则此行为表名  
            Set table = mdl.Tables.CreateNew     '创建表  
            table.Name = Split(.Cells(rowNum , 1).Value, ":")(1) '指定表名 
            table.Code = Split(.Cells(rowNum , 1).Value, ":")(1)
            table.Comment = Split(.Cells(rowNum , 1).Value, ":")(0) '指定表注释  
            count = count + 1
        Else
            '创建一个字段
            Set col = table.Columns.CreateNew
            
            '字段名
            col.Name = .Cells(rowNum, colNameIndex).Value
            '字段编码
            col.Code = .Cells(rowNum, colNameIndex).Value
            
            '字段类型
            col.DataType = .Cells(rowNum, colTypeIndex).Value
            If CStr(.Cells(rowNum, colNameIndex).Value) <> "" And (CStr(.Cells(rowNum, colTypeIndex).Value) = "varchar" Or CStr(.Cells(rowNum, colTypeIndex).Value) = "char") Then
                col.DataType = col.DataType + "(" + CStr(.Cells(rowNum, colTypeLengthIndex).Value) + ")"
            End If
            
            '字段注释
            col.Comment = .Cells(rowNum, colCommentIndex).Value
            If .Cells(rowNum, colMultiValueIndex).Value <> "" Then
                col.Comment = col.Comment + "。" + .Cells(rowNum, colMultiValueIndex).Value
            End If
            
            '主键
            If .Cells(rowNum, colIsPrimaryKeyIndex).Value = "是" Then
                col.Primary = True
            End If
            
            '唯一
            If .Cells(rowNum, colIsNotNullIndex).Value = "是" Then
                col.Mandatory = True
            End If
            
            '默认值
            If CStr(.Cells(rowNum, colDefaultValueIndex).Value) <> "" Then
                col.DefaultValue = CStr(.Cells(rowNum, colDefaultValueIndex).Value)
            End If
        End If
    End If
    
    
End With

Next
MsgBox "生成数据表结构共计 " + CStr(count) + "个"
xlsWorkBook.Close
x1sApp.Quit
Set x1sApp = Nothing
Set xlsWorkBook = Nothing

Exit Sub
End Sub

使用

在powerdesigner创建一个pdm模型,然后ctrl+shift+xtools->execute commands->edit/run script打开脚本输入框,粘贴并运行即可。

注意

表格示例(本来想直接复制图片的,但太麻烦,所以这里用markdown代替excel)
用户表:user

字段名 字段说明 类型 长度 是否主键 是否唯一 默认值 多值
id 唯一标识 varchar 32
login_id 登录ID varchar 32
username 用户名 varchar 50
password 密码 varchar 50
status 状态 int 1 1 0:禁用;1:启用

说明
表格不一定要按照这个来,可以参考代码的逻辑,自行定制。

猜你喜欢

转载自blog.csdn.net/cblstc/article/details/102585777