Powerdesigner_数据结构_导出excel

'******************************************************************************
Option Explicit
Dim rowsNum
rowsNum = 0
'-----------------------------------------------------------------------------
' Main function
'-----------------------------------------------------------------------------
' Get the current active model
Dim Model
Set Model = ActiveModel
If (Model Is Nothing) Or (Not Model.IsKindOf(PdPDM.cls_Model)) Then
    MsgBox "The current model is not an PDM model."
Else
    Dim tableCount, maxTableNum, tableNames
    tableCount = Model.tables.count
    maxTableNum = tableCount - 1

    tableNames = SortTableList(Model, tableCount, maxTableNum)

    '创建EXCEL APP
    dim beginrow
    DIM EXCEL, colsSheet
    set EXCEL = CREATEOBJECT("Excel.Application")
    EXCEL.workbooks.add(-4167)'添加工作表
    EXCEL.workbooks(1).sheets(1).name ="表结构"
    set colsSheet = EXCEL.workbooks(1).sheets("表结构")
    ' tables properties list
    ShowProperties Model, colsSheet, tableNames, maxTableNum
    EXCEL.workbooks(1).Sheets(1).Select
    EXCEL.visible = true
    colsSheet.Columns(1).ColumnWidth = 20
    colsSheet.Columns(2).ColumnWidth = 20
    colsSheet.Columns(3).ColumnWidth = 40
    colsSheet.Columns(4).ColumnWidth = 10
    colsSheet.Columns(5).ColumnWidth = 10
    '不显示网格线
    EXCEL.ActiveWindow.DisplayGridlines = False

    '*  MsgBox "hello world."
End If

'-----------------------------------------------------------------------------
' Show properties of tables
'-----------------------------------------------------------------------------
Sub ShowProperties(mdl, colsSheet, tableNames, maxTableNum)
    ' Show tables of the current model/package
    rowsNum = 0
    beginrow = rowsNum + 1
    Dim rowIndex
    rowIndex = 3
    ' For each table
    output "show table properties begin"
    'Dim tab
    'For Each tab In mdl.tables
    '   ShowTable tab, colsSheet, rowIndex, tablesSheet
    '   rowIndex = rowIndex +1
    'Next
    'if mdl.tables.count > 0 then
    '    colsSheet.Range("A" & beginrow + 1 & ":A" & rowsNum).Rows.Group
    'end if
    Dim i, pos
    For i = 0 To maxTableNum
        pos = tableNames(1, i)
        ShowTable mdl.tables.Item(pos), colsSheet, rowIndex
        rowIndex = rowIndex +1
    Next

    output "end"
End Sub

'-----------------------------------------------------------------------------
' Show table properties
'-----------------------------------------------------------------------------
Sub ShowTable(tab, colsSheet, rowIndex)
    If IsObject(tab) Then
        Dim rangFlag
        rowsNum = rowsNum + 1
        ' Show properties
        Output "================================"
        colsSheet.cells(rowsNum, 1) =tab.name
        colsSheet.cells(rowsNum, 1).HorizontalAlignment=3
        colsSheet.Range(colsSheet.cells(rowsNum, 1),colsSheet.cells(rowsNum, 5)).Merge

        rowsNum = rowsNum + 1

        colsSheet.cells(rowsNum, 1) = "字段名"
        colsSheet.cells(rowsNum, 2) = "字段类型"
        colsSheet.cells(rowsNum, 3) = "注释"
        colsSheet.cells(rowsNum, 4) = "是否主键"
        colsSheet.cells(rowsNum, 5) = "是否非空"
        '设置边框 字体为10号 加粗
        colsSheet.Range(colsSheet.cells(rowsNum-1, 1), colsSheet.cells(rowsNum, 5)).Borders.LineStyle = "1"
        colsSheet.Range(colsSheet.cells(rowsNum-1, 1), colsSheet.cells(rowsNum, 5)).Font.Size = 10
        colsSheet.Range(colsSheet.cells(rowsNum-1, 1), colsSheet.cells(rowsNum, 5)).Font.Bold = True
        
        Dim col ' running column
        Dim colsNum
        colsNum = 0
        for each col in tab.columns
            rowsNum = rowsNum + 1
            colsNum = colsNum + 1
            colsSheet.cells(rowsNum, 1) = col.code
            colsSheet.cells(rowsNum, 2) = col.datatype
            colsSheet.cells(rowsNum, 3) = col.name
            If col.Primary = true Then
                colsSheet.cells(rowsNum, 4) = "Y"
            Else
                colsSheet.cells(rowsNum, 4) = " "
            End If
            If col.Mandatory = true Then
                colsSheet.cells(rowsNum, 5) = "Y"
            Else
                colsSheet.cells(rowsNum, 5) = " "
            End If
        next
        colsSheet.Range(colsSheet.cells(rowsNum - colsNum + 1, 1), colsSheet.cells(rowsNum, 5)).Borders.LineStyle = "3"
        colsSheet.Range(colsSheet.cells(rowsNum - colsNum + 1, 1), colsSheet.cells(rowsNum, 5)).Font.Size = 10
        rowsNum = rowsNum + 2

        Output "FullDescription: "       + tab.Name
    End If
End Sub

'-----------------------------------------------------------------------------
' Get Table List Sort By Name
'-----------------------------------------------------------------------------
Function SortTableList(mdl, count, maxNum)
    Dim tabs()
    ReDim tabs(2, count)
    Dim tab, i, j, temp

    'output "get table names"
    For i = 0 To maxNum
        temp = mdl.tables.Item(i).number
        tabs(0, i) = temp
        'output temp
    Next

    'output "sort table names"
    For i = 0 To maxNum
        For j = i + 1 To maxNum
            If tabs(0, j) < tabs(0, i) Then
                temp = tabs(0, i)
                tabs(0, i) = tabs(0, j)
                tabs(0, j) = temp
            End If
        Next
    Next

    'output "put table index to sorted names"
    i = 0
    For Each tab In Model.tables
        temp = tab.number
        'output tab.name
        For j = 0 To maxNum
            If (tabs(0, j)) = temp Then
                tabs(1, j) = i
            End If
        Next
        i = i + 1
    Next
    'For i = 0 To maxNum
    '    output tabs(0, i) + "-" & tabs(1, i)
    'Next
    SortTableList = tabs
End Function

保存为.vbs格式即可使用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值