powerdesigner 使用excel 批量导入创建表

前言

因为公司要从 oracle 迁移到 mysql,同时也没有关于数据库的文档,同时数据库表太多了,一个一个手动建模,太没有效率了,所以网上查了一下,powerdesigner 使用的 visual basic 语言,可以使用脚本批量导入创建。

如何使用脚本

在这里插入图片描述
在这里插入图片描述

excel 样式

在这里插入图片描述

vbs 脚本

' 第一行是表头
' 第二行开始是列的描述,分别是:表名、表注释、列名、列数据类型、列数据长度、列是否为空、列注释
' Excel的sheet名称统一为Sheet1
'开始
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\hp\Desktop\sql.xlsx" '指定 excel文档路径
    x1.Workbooks(1).Worksheets("Sheet1").Activate '指定要打开的sheet名称
Else
    HaveExcel = False
End If
 
a x1, mdl
 
sub a(x1, mdl)

dim rwIndex 
dim table
dim tableName
dim colname
dim col
dim dataType
dim tableComment
 
on error Resume Next

set tableName = ""
set tableComment = ""

For rwIndex = 2 To 9713 '
    With x1.Workbooks(1).Worksheets("Sheet1")
        If tableName = "" Then
            ' 创建表
            set table = mdl.Tables.CreateNew '创建一个 表实体
            tableName = .Cells(rwIndex, 1).Value '表名
            tableComment = .Cells(rwIndex, 2).Value
            If tableComment = "" Then 
                table.Name=tableName '表名
            Else 
                table.Name=tableComment '表名
            End If
            table.Code=.Cells(rwIndex, 1).Value '表标识
            table.Comment=tableComment '表注释
        ElseIf tableName = .Cells(rwIndex, 1).Value Then

        Else
            set table = mdl.Tables.CreateNew '创建一个 表实体
            tableName = .Cells(rwIndex, 1).Value '表名
            table.Name=.Cells(rwIndex, 1).Value '表名
            table.Code=.Cells(rwIndex, 1).Value '表标识
            table.Comment=.Cells(rwIndex, 2).Value '表注释
        End If

        ' 创建列
        set col = table.Columns.CreateNew '创建一列/字段
        If .Cells(rwIndex, 7).Value = "" Then
            col.Name = .Cells(rwIndex, 3).Value '指定列名
        Else
            col.Name = .Cells(rwIndex, 7).Value '指定列名
        End If
        col.Code = .Cells(rwIndex, 3).Value
        col.Comment = .Cells(rwIndex, 7).Value '指定列说明

        dataType = .Cells(rwIndex, 4).Value '指定列数据类型
        Select Case dataType
        Case "DATE"
            col.DataType = "datetime"
        CASE "VARCHAR2"
            col.DataType = Replace("varchar(%s)","%s", .Cells(rwIndex, 5).Value)
        CASE "NVARCHAR2"
            col.DataType = Replace("varchar(%s)","%s", .Cells(rwIndex, 5).Value)
        CASE "CHAR"
            col.DataType = Replace("char(%s)","%s", .Cells(rwIndex, 5).Value)
        CASE "CLOB"
            col.DataType = "text"
        CASE "BLOB"
            col.DataType = "BLOB"
        CASE "FLOAT"
            col.DataType = Replace("float(%s)","%s", .Cells(rwIndex, 5).Value)
        CASE "NUMBER"
            col.DataType = "bigint"
        CASE "RAW"
            col.DataType = Replace("binary(%s)","%s", .Cells(rwIndex, 5).Value)
        CASE "TIMESTAMP(6)"
            col.DataType = "timestamp"
        END Select
    
        If .Cells(rwIndex, 3).Value = "ID" Then
            col.Primary = true '指定列是否主键 true 为主键 '指定列是否可空 true 为不可空  
        End If

		If .Cells(rwIndex, 6).Value = "Y" Then
            col.Mandatory = true '指定列是否主键 true 为主键 
        End If
    End With
Next
MsgBox "生成成功"
 
Exit Sub
End sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值