其他分享
首页 > 其他分享> > PowerDesigner导出表结构到Excel

PowerDesigner导出表结构到Excel

作者:互联网

打开PowerDesigner,找到创建的物理模型(Physical Data Model), 使用快捷键,或者 在PowerDesigner菜单栏中,依次点击“Tools ->Excute Commands->Edit/Run Script..” 将下面代码粘贴到空白,点击Run 将导出的结果(Excel)保存到自己的位置

 

 

 

 

'shexunyu

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
      ' Get the tables collection
      ' 创建EXCEL APP
      dim beginrow
      DIM EXCEL, SHEET, SHEETLIST
      set EXCEL = CREATEOBJECT("Excel.Application")
      EXCEL.workbooks.add(-4167)    ' 添加工作表Sheet1
      EXCEL.workbooks(1).sheets(1).name ="表结构"      'Sheet1名称
      set SHEET = EXCEL.workbooks(1).sheets("表结构")  'Sheet1对象
        
      EXCEL.workbooks(1).sheets.add     ' 添加工作表Sheet2
      EXCEL.workbooks(1).sheets(1).name ="目录"           'Sheet2名称
      set SHEETLIST = EXCEL.workbooks(1).sheets("目录")   'Sheet2对象
      ShowTableList Model,SHEETLIST
  
      ShowProperties Model, SHEET,SHEETLIST
       
            output  "Select: " + EXCEL.workbooks(1).Sheets(2).name         
      EXCEL.workbooks(1).Sheets(2).Select       '选择默认打开的工作表
      EXCEL.visible = true  ' 弹出Excel工作簿
      ' 设置列宽
      sheet.Columns(1).ColumnWidth = 30
      sheet.Columns(2).ColumnWidth = 20
      sheet.Columns(3).ColumnWidth = 20
      sheet.Columns(4).ColumnWidth = 30
      '根据需要添加列数,这里是4列,接着是自动换行
      sheet.Columns(1).WrapText =true
      sheet.Columns(2).WrapText =true
      sheet.Columns(4).WrapText =true
      '不显示网格线
      EXCEL.ActiveWindow.DisplayGridlines = True
 End If
              
'-----------------------------------------------------------------------------
' Show properties of tables
'-----------------------------------------------------------------------------
Sub ShowProperties(mdl, sheet,SheetList)
   ' Show tables of the current model/package
   rowsNum=0
   beginrow = rowsNum+1
   Dim rowIndex ' 为目录Sheet设置链接位置
   rowIndex=3
   ' For each table
   output "表结构 begin ========================"
   Dim tab  ' Power Tables
   For Each tab In mdl.tables
      ShowTable tab,sheet,rowIndex,sheetList
      rowIndex = rowIndex +1
   Next
    
   if mdl.tables.count > 0 then
        sheet.Range("A" & beginrow + 1 & ":A" & rowsNum).Rows.Group
   end if
    
   output "表结构 end !"
End Sub
 
'-----------------------------------------------------------------------------
' Show table properties
'-----------------------------------------------------------------------------
Sub ShowTable(tab, sheet,rowIndex,sheetList)
   If IsObject(tab) Then
         
        rowsNum = rowsNum + 1   'rowsNum=1,工作表第一行
        ' Show properties
        Output ":表"+ tab.name +"的结构"
        sheet.cells(rowsNum, 1) =tab.name
        sheet.cells(rowsNum, 1).HorizontalAlignment = 1 '1左对齐 2右对齐 3居中
        sheet.cells(rowsNum, 2) = tab.code
        sheet.cells(rowsNum, 3) = tab.comment
        sheet.Range(sheet.cells(rowsNum, 4),sheet.cells(rowsNum, 7)).Merge ' 单元格合并
       
        '设置超链接,从目录点击表名去查看表结构
        sheetList.Hyperlinks.Add sheetList.cells(rowIndex,2), "","表结构"&"!B"&rowsNum
         
        '字段名称    字段编码    数据类型    注释   
        rowsNum = rowsNum + 1   'rowsNum=2,工作表第2行
        sheet.cells(rowsNum, 1) = "字段名称"
        sheet.cells(rowsNum, 2) = "字段编码"
        sheet.cells(rowsNum, 3) = "数据类型"
        sheet.cells(rowsNum, 4) = "注释"
        sheet.cells(rowsNum, 5) = "是否主键"
        sheet.cells(rowsNum, 6) = "必须"
        sheet.cells(rowsNum, 7) = "默认值"
        '设置边框
        sheet.Range(sheet.cells(rowsNum-1, 1),sheet.cells(rowsNum, 7)).Borders.LineStyle = "1"
        '字体为10号
        sheet.Range(sheet.cells(rowsNum-1, 1),sheet.cells(rowsNum, 7)).Font.Size=10
        Dim col ' running column
        Dim colsNum
        colsNum = 0
        for each col in tab.columns
            rowsNum = rowsNum + 1
            colsNum = colsNum + 1
            sheet.cells(rowsNum, 1) = col.name
            sheet.cells(rowsNum, 2) = col.code
            sheet.cells(rowsNum, 3) = col.datatype
            sheet.cells(rowsNum, 4) = col.comment  
            '5是否主键,6约束是否为Null,7默认值
            If col.Primary = true Then
               sheet.cells(rowsNum, 5) = "Y"
            Else
               sheet.cells(rowsNum, 5) = " "
            End If
            If col.Mandatory = true Then
               sheet.cells(rowsNum, 6) = "Y"
            Else
               sheet.cells(rowsNum, 6) = " "
            End If
               sheet.cells(rowsNum, 7) =  col.defaultvalue
        next
        sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,7)).Borders.LineStyle = "3"    ' Style3边框线为虚线 
        sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,7)).Font.Size = 10
        rowsNum = rowsNum + 2   ' 表间隔为2行
        
   End If
     
End Sub
 
'-----------------------------------------------------------------------------
' Show List Of Table
'-----------------------------------------------------------------------------
Sub ShowTableList(mdl, SheetList)
   ' Show tables of the current model/package
   Dim rowsNo
   rowsNo=1
   output "目录begin"
   SheetList.cells(rowsNo, 1) = "主题"
   SheetList.cells(rowsNo, 2) = "表名称"
   SheetList.cells(rowsNo, 3) = "表编码"
   SheetList.cells(rowsNo, 4) = "表说明"
   rowsNo = rowsNo + 1
   SheetList.cells(rowsNo, 1) = mdl.name
   ' For each table
   Dim tab
   For Each tab In mdl.tables
     If IsObject(tab) Then
        rowsNo = rowsNo + 1
                SheetList.cells(rowsNo, 1) = ""
                SheetList.cells(rowsNo, 2) = tab.name
                SheetList.cells(rowsNo, 3) = tab.code
                SheetList.cells(rowsNo, 4) = tab.comment
     End If
   Next
    
    SheetList.Columns(1).ColumnWidth = 20
    SheetList.Columns(2).ColumnWidth = 20
    SheetList.Columns(3).ColumnWidth = 30
    SheetList.Columns(4).ColumnWidth = 60
   output "目录end"
End Sub
View Code

 

 

 

 

 

标签:SheetList,sheet,rowsNum,Excel,cells,导出,PowerDesigner,tab,rowsNo
来源: https://www.cnblogs.com/shexunyu/p/15568380.html