1. 程式人生 > >PowerDesigner導出表為Excel(轉)

PowerDesigner導出表為Excel(轉)

*** tps 顯示 創建 lld collect box lin confirm

打開腳本運行器Ctrl+Shift+X

導出:

******************************************************************************
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)添加工作表 EXCEL.workbooks(1).sheets(1).name ="表結構" set SHEET = EXCEL.workbooks(1).sheets("表結構") EXCEL.workbooks(
1).sheets.add EXCEL.workbooks(1).sheets(1).name ="目錄" set SHEETLIST = EXCEL.workbooks(1).sheets("目錄") ShowTableList Model,SHEETLIST ShowProperties Model, SHEET,SHEETLIST EXCEL.workbooks(1).Sheets(2).Select EXCEL.visible = true設置列寬和自動換行 sheet.Columns(1).ColumnWidth = 20 sheet.Columns(2).ColumnWidth = 20 sheet.Columns(3).ColumnWidth = 20 sheet.Columns(4).ColumnWidth = 40 sheet.Columns(5).ColumnWidth = 10 sheet.Columns(6).ColumnWidth = 10 sheet.Columns(1).WrapText =true sheet.Columns(2).WrapText =true sheet.Columns(4).WrapText =true
不顯示網格線 EXCEL.ActiveWindow.DisplayGridlines = False 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 rowIndex=3 ‘ For each table output "begin" Dim tab 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 Dim rangFlag rowsNum = rowsNum + 1 ‘ Show properties Output "================================" sheet.cells(rowsNum, 1) =tab.name sheet.cells(rowsNum, 1).HorizontalAlignment=3 sheet.cells(rowsNum, 2) = tab.code ‘sheet.cells(rowsNum, 5).HorizontalAlignment=3 ‘sheet.cells(rowsNum, 6) = "" ‘sheet.cells(rowsNum, 7) = "表說明" sheet.cells(rowsNum, 3) = tab.comment sheet.cells(rowsNum, 8).HorizontalAlignment=3 sheet.Range(sheet.cells(rowsNum, 3),sheet.cells(rowsNum, 7)).Merge 設置超鏈接,從目錄點擊表名去查看表結構 字段中文名 字段英文名 字段類型 註釋 是否主鍵 是否非空 默認值 sheetList.Hyperlinks.Add sheetList.cells(rowIndex,2), "","表結構"&"!B"&rowsNum rowsNum = rowsNum + 1 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" ‘sheet.Range(sheet.cells(rowsNum-1, 4),sheet.cells(rowsNum, 9)).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, 3) = "" ‘sheet.cells(rowsNum, 4) = col.name sheet.cells(rowsNum, 2) = col.code sheet.cells(rowsNum, 3) = col.datatype sheet.cells(rowsNum, 4) = col.comment 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" ‘sheet.Range(sheet.cells(rowsNum-colsNum+1,4),sheet.cells(rowsNum,9)).Borders.LineStyle = "3" sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,7)).Font.Size = 10 rowsNum = rowsNum + 2 Output "FullDescription: " + tab.Name End If End Sub ----------------------------------------------------------------------------- Show List Of Table ----------------------------------------------------------------------------- Sub ShowTableList(mdl, SheetList) ‘ Show tables of the current model/package Dim rowsNo rowsNo=1 ‘ For each table 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 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

導入:

******************************************************************************
開始
Option Explicit
Dim tab_name,tab_code,tab_comment
Dim b_r, e_r, s_r, j, m, n
Dim mdl  the current model
dim count
Dim HaveExcel
Dim RQ
Dim file_name,WScript
Set mdl = ActiveModel
If (mdl Is Nothing) Then
   MsgBox "There is no Active Model"
End If
RQ = vbYes ‘MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")
If RQ = vbYes Then
   HaveExcel = True
    Open & Create Excel Document
Else
   HaveExcel = False
End If
file_name = selectFile() 
if file_name <> "" then
   Dim x1  
   Set x1 = CreateObject("Excel.Application")
   x1.Workbooks.Open file_name    指定excel文檔路徑
   x1.Workbooks(1).Worksheets("表結構").Activate   指定要打開的sheet名稱
j = 1
do while n < 11
if x1.Workbooks(1).Worksheets("表結構").cells(j,1).value <> "" then
call a(x1, mdl, j, getRow(x1, j))
msgbox j & "--" & getRow(x1, j)
count = count + 1
j = getRow(x1, j)
n = 0
end if
j = j + 1
n = n + 1
loop
‘MsgBox "生成數據表結構共計 " + CStr(count), vbOK + vbInformation, "表 導入完畢!"
MsgBox "生成數據表結構共計 " & CStr(count) & " 表導入完畢!"
x1.Workbooks(1).close
x1.quit
else
msgbox "沒有選擇文件!"
end if

sub a(x1, mdl, r_0,r_9)
dim rwIndex   
dim tableName
dim colname
dim table
dim col

tab_name = ucase(x1.Workbooks(1).Worksheets("表結構").cells(r_0,1).value)    ‘指定表名,如果在Excel文檔裏有,也可以 .Cells(rwIndex, 3).Value 這樣指定
tab_code = ucase(x1.Workbooks(1).Worksheets("表結構").cells(r_0,2).value)  指定表名
tab_comment = ucase(x1.Workbooks(1).Worksheets("表結構").cells(r_0,3).value)

on error Resume Next
set table = mdl.Tables.CreateNew ‘創建一個表實體
table.Name = tab_name
table.Code = tab_code
table.Comment = tab_comment

For rwIndex = r_0 + 2 To r_9   指定要遍歷的Excel行標  由於第1行是表頭,從第2行開始
        With x1.Workbooks(1).Worksheets("表結構")
            If .Cells(rwIndex, 2).Value = "" Then
               Exit For
            End If
               set col = table.Columns.CreateNew   ‘創建一列/字段
               ‘MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列"
               If .Cells(rwIndex, 1).Value = "" Then
                  col.Name = ucase(.Cells(rwIndex, 2).Value)   ‘指定列名
               Else 
                  col.Name = ucase(.Cells(rwIndex, 1).Value)
               End If
               ‘MsgBox col.Name, vbOK + vbInformation, "列"
               col.Code = ucase(.Cells(rwIndex, 2).Value)   ‘指定列名
               
               col.DataType = ucase(.Cells(rwIndex, 3).Value)   指定列數據類型
               
               col.Comment = ucase(.Cells(rwIndex, 4).Value)  ‘指定列說明
               
               If ucase(.Cells(rwIndex, 5).Value) = "Y" Then
                   col.Primary = true    指定主鍵
                   col.defaultvalue = ucase(.Cells(rwIndex,7).value)
               End If 
               If ucase(.Cells(rwIndex,6).value) = "Y" then
                   col.defaultvalue = ucase(.Cells(rwIndex,7).value)
                   col.Mandatory = true                
               End If
               
        End With
Next
End sub

Function getRow(x1, s_r)
dim i, k
k = s_r
do while x1.Workbooks(1).Worksheets("表結構").cells(k,1).value <> ""
k = k + 1
if x1.Workbooks(1).Worksheets("表結構").cells(k,1).value = "" then
getRow = k - 1
exit function
end if
loop
End Function
Function SelectFile()
    Dim shell : Set shell = CreateObject("WScript.Shell")
    Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
    Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
    Dim tempName : tempName = fso.GetTempName()
    Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
    tempFile.Write _
    "<html>" & _
    "<head>" & _
    "<title>Browse</title>" & _
    "</head>" & _
    "<body>" & _
    "<input type=‘file‘ id=‘f‘ />" & _
    "<script type=‘text/javascript‘>" & _
    "var f = document.getElementById(‘f‘);" & _
    "f.click();" & _
    "var shell = new ActiveXObject(‘WScript.Shell‘);" & _
    "shell.RegWrite(‘HKEY_CURRENT_USER\\Volatile Environment\\MsgResp‘, f.value);" & _
    "window.close();" & _
    "</script>" & _
    "</body>" & _
    "</html>"
    tempFile.Close
    shell.Run tempFolder & "\" & tempName & ".hta", 0, True
    SelectFile = shell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp")
    shell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp"
End Function

其它:

所有表同在一個Sheet

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
 set EXCEL = CREATEOBJECT("Excel.Application")
 EXCEL.workbooks.add(-4167)‘添加工作表
 EXCEL.workbooks(1).sheets(1).name ="test"
 set sheet = EXCEL.workbooks(1).sheets("test")

 ShowProperties Model, SHEET
 EXCEL.visible = true
 設置列寬和自動換行
 sheet.Columns(1).ColumnWidth = 20 
 sheet.Columns(2).ColumnWidth = 40 
 sheet.Columns(4).ColumnWidth = 20 
 sheet.Columns(5).ColumnWidth = 20 
 sheet.Columns(6).ColumnWidth = 15 
 sheet.Columns(1).WrapText =true
 sheet.Columns(2).WrapText =true
 sheet.Columns(4).WrapText =true
 End If
-----------------------------------------------------------------------------
 Show properties of tables
-----------------------------------------------------------------------------
Sub ShowProperties(mdl, sheet)
   ‘ Show tables of the current model/package
   rowsNum=0
   beginrow = rowsNum+1
   ‘ For each table
   output "begin"
   Dim tab
   For Each tab In mdl.tables
      ShowTable tab,sheet
   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)
   If IsObject(tab) Then
     Dim rangFlag
     rowsNum = rowsNum + 1
      ‘ Show properties
      Output "================================"
      sheet.cells(rowsNum, 1) = "實體名"
      sheet.cells(rowsNum, 2) =tab.name
      sheet.cells(rowsNum, 3) = ""
      sheet.cells(rowsNum, 4) = "表名"
      sheet.cells(rowsNum, 5) = tab.code
      sheet.Range(sheet.cells(rowsNum, 5),sheet.cells(rowsNum, 6)).Merge
      rowsNum = rowsNum + 1
      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.Range(sheet.cells(rowsNum-1, 1),sheet.cells(rowsNum, 2)).Borders.LineStyle = "1"
      sheet.Range(sheet.cells(rowsNum-1, 4),sheet.cells(rowsNum, 6)).Borders.LineStyle = "1"
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.comment
        sheet.cells(rowsNum, 3) = ""
      sheet.cells(rowsNum, 4) = col.name
      sheet.cells(rowsNum, 5) = col.code
      sheet.cells(rowsNum, 6) = col.datatype
      next
      sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,2)).Borders.LineStyle = "2"       
      sheet.Range(sheet.cells(rowsNum-colsNum+1,4),sheet.cells(rowsNum,6)).Borders.LineStyle = "2"
      rowsNum = rowsNum + 1

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

每個表一個Sheet

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, BOOK, SHEET  
 Set EXCEL = CreateObject("Excel.Application")  
 EXCEL.Visible = True  
 Set BOOK = EXCEL.Workbooks.Add(-4167) ‘新建工作簿  
  
 BOOK.Sheets(1).Name = "數據庫表結構"  
 Set SHEET = EXCEL.workbooks(1).sheets("數據庫表結構")  
  
 ShowProperties Model, SHEET  
 EXCEL.visible = true   
 設置列寬和自動換行   
 SHEET.Columns(1).ColumnWidth = 10     
 SHEET.Columns(2).ColumnWidth = 30     
 SHEET.Columns(3).ColumnWidth = 20     
  
 SHEET.Columns(1).WrapText =true   
 SHEET.Columns(2).WrapText =true   
 SHEET.Columns(3).WrapText =true   
  
End If  
  
-----------------------------------------------------------------------------   
 Show properties of tables   
-----------------------------------------------------------------------------   
Sub ShowProperties(mdl, sheet)   
   ‘ Show tables of the current model/package   
   rowsNum=0   
   beginrow = rowsNum+1   
   ‘ For each table   
   output "begin"   
   Dim tab   
   For Each tab In mdl.tables   
      ShowTable tab,sheet   
   Next   
   if mdl.tables.count > 0 then   
        sheet.Range("A" & beginrow + 1 & ":A" & rowsNum).Rows.Group   
   end if   
   output "end"   
End Sub  
  
‘-----------------------------------------------------------------------------   
‘ 數據表查詢   
‘-----------------------------------------------------------------------------  
Sub ShowTable(tab, sheet)     
   If IsObject(tab) Then   
     Dim rangFlag  
      sheet.cells(1, 1) = "序號"   
      sheet.cells(1, 2) = "表名"  
      sheet.cells(1, 3) = "實體名"  
      ‘設置邊框   
      sheet.Range(sheet.cells(1, 1),sheet.cells(1, 3)).Borders.LineStyle = "1"  
      設置背景顏色  
      sheet.Range(sheet.cells(1, 1),sheet.cells(1, 3)).Interior.ColorIndex = "19"  
  
      rowsNum = rowsNum + 1  
      sheet.cells(rowsNum+1, 1) = rowsNum   
      sheet.cells(rowsNum+1, 2) = tab.code  
      sheet.cells(rowsNum+1, 3) = tab.name  
      設置邊框  
      sheet.Range(sheet.cells(rowsNum+1,1),sheet.cells(rowsNum+1,3)).Borders.LineStyle = "2"  
  
      增加Sheet  
      BOOK.Sheets.Add , BOOK.Sheets(BOOK.Sheets.count)  
      BOOK.Sheets(rowsNum+1).Name = tab.code   
  
      Dim shtn  
      Set shtn = EXCEL.workbooks(1).sheets(tab.code)  
      設置列寬和換行  
       shtn.Columns(1).ColumnWidth = 30     
       shtn.Columns(2).ColumnWidth = 20     
       shtn.Columns(3).ColumnWidth = 20  
       shtn.Columns(5).ColumnWidth = 30     
       shtn.Columns(6).ColumnWidth = 20     
  
       shtn.Columns(1).WrapText =true   
       shtn.Columns(2).WrapText =true   
       shtn.Columns(3).WrapText =true  
       shtn.Columns(5).WrapText =true   
       shtn.Columns(6).WrapText =true  
  
       設置列標題  
       shtn.cells(1, 1) = "字段中文名"   
       shtn.cells(1, 2) = "字段名"  
       shtn.cells(1, 3) = "字段類型"  
       shtn.cells(1, 5) = tab.code  
       shtn.cells(1, 6) = tab.Name  
       設置邊框   
       shtn.Range(shtn.cells(1, 1),shtn.cells(1, 3)).Borders.LineStyle = "1"  
       shtn.Range(shtn.cells(1, 5),shtn.cells(1, 6)).Borders.LineStyle = "1"  
       設置背景顏色  
       shtn.Range(shtn.cells(1, 1),shtn.cells(1, 3)).Interior.ColorIndex = "19"  
       shtn.Range(shtn.cells(1, 5),shtn.cells(1, 6)).Interior.ColorIndex = "19"  
  
      Dim col  running column   
      Dim colsNum  
      Dim rNum   
      colsNum = 0  
      rNum = 0   
            for each col in tab.columns   
              rNum = rNum + 1   
              colsNum = colsNum + 1   
  
            shtn.cells(rNum+1, 1) = col.name   
            shtn.cells(rNum+1, 2) = col.code   
            shtn.cells(rNum+1, 3) = col.datatype   
            next   
            shtn.Range(shtn.cells(rNum-colsNum+2,1),shtn.cells(rNum+1,3)).Borders.LineStyle = "2"           
            rNum = rNum + 1   
  
            Output "FullDescription: "       + tab.Name  
  
   End If     
End Sub  

參考:

https://www.cnblogs.com/chenz/articles/3456340.html(以上內容轉自此篇文章)

https://blog.csdn.net/Bin594505536/article/details/50517573(以上內容轉自此篇文章)

PowerDesigner導出表為Excel(轉)