PowerDesigner數據庫設計PDM基於Excel的導入導出總結

  常常用到pdm來管理代碼,一兩張表,手寫一下還湊合,一旦表多了,就慌了。因而,開始學習用vbs進行Excel的來快速導入導出操做PDM就變得很緊急了,蒐羅了網絡上的不少vbs腳本,各有各的優勢,但對於我來講都不太理想,遂基於網絡的版本,改造了合適本身版本,本文就是基於網上的代碼進行總結。
  在數據庫建模中會用到Powerdesigner軟件進行表結構的設計,有時候咱們須要將Excel裏面的表結構導入到Powerdesigner中生成PDM模型文件,或者將Powerdesigner中已有的PDM模型導出生成Excel文檔;咱們能夠經過Powerdesigner的腳本定製功能,來實現Excel的導入導出。
 
在PowerDesigner 中 ctrl+shift+x 彈出執行腳本界面,輸入以下代碼就會生成 Excel,前三個代碼爲導出,最後一個爲基於Excel導入表結構
 代碼一(無超連接版本,來源於網絡):全部表結構都在一張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
複製代碼

 

代碼一:全部的表在同一個 Sheet 頁中
複製代碼
'******************************************************************************
'* File:     pdm2excel.txt
'* Title:    pdm export to excel
'* Purpose:  To export the tables and columns to Excel
'* Model:    Physical Data Model
'* Objects:  Table, Column, View
'* Author:   ziyan
'* Created:  2012-05-03
'* Version:  1.0
'******************************************************************************
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 頁,第一個 Sheet 頁上是全部表的列表數據庫

複製代碼
'****************************************************************************** 
'* File:     pdm2excel.txt 
'* Title:    pdm export to excel 
'* Purpose:  To export the tables and columns to Excel 
'* Model:    Physical Data Model 
'* Objects:  Table, Column, View 
'* Author:   Chirs 
'* Created:  2015-01-28 
'* Version:  1.0 
'****************************************************************************** 
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
複製代碼

 (推薦)代碼三(基於網絡版本修改改造,新增超連接等內容,更加友好):第一個 Sheet 頁上是全部表的目錄列表,每一個表都會新建一個 Sheet 頁網絡

'******************************************************************************
Option Explicit
   Dim rowsNum,tablesNum
   rowsNum = 0
   tablesNum = 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,SHEETLIST
      set EXCEL = CREATEOBJECT("Excel.Application")
      Set BOOK = EXCEL.Workbooks.Add(-4167) '新建工做簿
      BOOK.sheets(1).name ="目錄"
      set SHEETLIST = BOOK.sheets("目錄")
      ShowTableList Model,SHEETLIST

      ShowProperties Model,BOOK,SHEETLIST
      
      MsgBox "導出完成!"
      
 End If
'-----------------------------------------------------------------------------
' Show properties of tables
'-----------------------------------------------------------------------------
Sub ShowProperties(mdl,BOOK,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
      tablesNum = tablesNum +1
      ShowTable tab,BOOK,rowIndex,sheetList,tablesNum
      rowIndex = rowIndex +1
   Next
   if mdl.tables.count > 0 then
        BOOK.Sheets(BOOK.Sheets.count).Range("A" & beginrow + 1 & ":A" & rowsNum).Rows.Group
   end if
   output "end"
End Sub
'-----------------------------------------------------------------------------
' Show table properties
'-----------------------------------------------------------------------------
Sub ShowTable(tab, BOOK,rowIndex,sheetList,tablesNum)


   If IsObject(tab) Then
   
     
     Dim rangFlag,SHEET
     
     'EXCEL.workbooks.add(-4167)'添加工做表
     BOOK.Sheets.Add , BOOK.Sheets(BOOK.Sheets.count)
     BOOK.Sheets(BOOK.Sheets.count).Name = tab.code
     set SHEET = BOOK.sheets(tab.code)
      
     'EXCEL.workbooks(1).sheets.add(-4167)
      EXCEL.workbooks(1).Sheets(tablesNum).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
      
     rowsNum = 0
     rowsNum = rowsNum + 1
      ' Show properties
      Output "================================"
      sheet.cells(rowsNum, 1) ="返回目錄"
      sheet.cells(rowsNum, 2) =tab.name
      sheet.cells(rowsNum, 2).HorizontalAlignment=3
      sheet.cells(rowsNum, 3) = tab.code
      'sheet.cells(rowsNum, 5).HorizontalAlignment=3
      'sheet.cells(rowsNum, 6) = ""
      'sheet.cells(rowsNum, 7) = "表說明"
      sheet.cells(rowsNum, 4) = tab.comment
      'sheet.cells(rowsNum, 8).HorizontalAlignment=3
      sheet.Range(sheet.cells(rowsNum, 4),sheet.cells(rowsNum, 7)).Merge
      '設置超連接,從目錄點擊表名去查看錶結構
      '字段中文名    字段英文名    字段類型    註釋    是否主鍵    是否非空    默認值
      sheetList.Hyperlinks.Add sheetList.cells(rowIndex,2), "",tab.code&"!B"&rowsNum
      sheet.Hyperlinks.Add sheet.cells(rowsNum,1), "","目錄"&"!B"&rowIndex
      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

代碼四(基於網絡版本修改改造,還有提高空間)Excel導入:學習

'============================================================
'從Excel文件中導入PowerDesigner 物理數據模型
'
'注意:1,Excel表格中不能有合併的單元格
'      2,列之間不能有空行
'============================================================


Option Explicit

'============================================================
'私有全局變量。
'============================================================
Private CURRENT_MODEL_NAME
Private CURRENT_MODEL
Private TABLES
Private EXCEL_APP
Private FILE_PATH

CURRENT_MODEL_NAME = "Excel"
Set EXCEL_APP = CreateObject("Excel.Application")
FILE_PATH="C:\Users\Dell\Desktop\worknew\模板.xlsx"    '文件的絕對路徑

'檢查文件是否存在
If CheckFileExsistence() Then
   '檢查當前是否有已經打開的物理圖
   Call GetModelByName(CURRENT_MODEL)
   If CURRENT_MODEL Is Nothing Then
      MsgBox("請先打開一個名稱爲「" & CURRENT_MODEL_NAME & "」的物理數據模型(Physical Data Model),而後再進執行導入!")
   Else
      Set TABLES = CURRENT_MODEL.Tables
      '根據EXCEL表格建立模型
      ImportModels()
   End If
Else
   MsgBox "文件" + FILE_PATH + "不存在!"
End If


'============================================================
'導入模型
'============================================================
Sub ImportModels
   '打開Excel文件
   Dim Filename
   Dim ReadOnly
   EXCEL_APP.Workbooks.Open FILE_PATH

   Dim worksheets
   Dim worksheetCount
   Set worksheets = EXCEL_APP.Worksheets
   worksheetCount = worksheets.Count
   If worksheetCount <= 0 Then
      Exit Sub
   End If
   
   Dim index
   Dim currentSheet
   For index = 1 to worksheetCount
      Set currentSheet = worksheets(index)
      Call CreateTable(currentSheet)
   Next
   
   '關閉Excel文件
   EXCEL_APP.Workbooks.Close
   MsgBox "導入完成!"
End Sub


'============================================================
'建立表
'============================================================
Sub CreateTable(ByRef worksheet)
   Dim cells
   Set cells = worksheet.Cells
   Dim table
   
   '檢查具備相同名稱的表是否已經存在
   Call GetTableByName(table, worksheet.Name)
   If table Is Nothing Then
      Set table = TABLES.CreateNew
      'Set table = TABLES.CreateNew
      'table.Name = cells(1, 1).Value
      'table.Code = cells(2, 1).Value
      'table.Comment = cells(3, 1).Value
      table.Name = worksheet.Name
      table.Code = worksheet.Name
      table.Comment = worksheet.Name
   End If
   
   Dim index
   Dim rows
   Dim col
   Set rows = worksheet.Rows
   For index = 3 to 512
      If EXCEL_APP.WorksheetFunction.CountA(rows(index)) <= 0 Then
         Exit For
      End If
      
      If ((cells(index,1).Value = "") or (cells(index,2).Value = "Name" ))Then '第二列爲空的均可以忽略
            'MsgBox "值2"
         'continue    '這裏忽略空行和表名行、表頭行
        Else 
            set col =table.Columns.CreateNew '建立一列/字段
         col.Code = cells(index, 1).Value    '指定列code
         'MsgBox "值3"
         col.DataType = cells(index, 3).Value    '指定列數據類型
         If cells(index, 4).Value = "Y" Then'指定主鍵
                col.Primary =true
         Else
            If cells(index, 5).Value = "" Then'指定列是否可空 true 爲不可空
                   col.Mandatory =true
            
            End If
            
            End If
            col.Name = cells(index, 2).Value    '指定列name            
            col.Comment = cells(index, 2).Value  '指定列說明
         'count = count + 1
        End If    
   Next
End Sub


'============================================================
'檢查文件是否存在
'============================================================
Function CheckFileExsistence
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   CheckFileExsistence = fso.FileExists(FILE_PATH)
End Function


'============================================================
'根據數據類型名稱,精度和刻度生成數據類型
'============================================================
Function GenerateDataType(dataTypeName, precision, scale)
   Select Case Ucase(dataTypeName)
      Case Empty
         GenerateDataType = Empty
      Case "NUMBER"
         GenerateDataType = "NUMBER(" & precision & "," & scale & ")"
   End Select
End Function


'============================================================
'獲取指定指定名稱的數據模型
'============================================================
Sub GetModelByName(ByRef model)
   Dim md
   For Each md in Models
      If StrComp(md.Name, CURRENT_MODEL_NAME) = 0 Then
         Set model = md
         Exit Sub
      End If
   Next
   Set model = Nothing
End Sub


'============================================================
'根據表名稱獲取對應的表
'============================================================
Sub GetTableByName(ByRef table, tableName)
   Dim tb
   For Each tb in TABLES
      If StrComp(tb.Name, tableName) = 0 Then
         Set table = tb
         Exit Sub
      End If
   Next
   Set table = Nothing
End Sub


'============================================================
'檢查字段是否已經存在
'============================================================
Function ColumnExists(ByRef table, columnName)
   Dim col
   For Each col in table.Columns
      If StrComp(col.Name, columnName) = 0 Then
         ColumnExists = True
         Exit Function
      End If
   Next
   ColumnExists = False
End Function

Excel導入模板字體

相關文章
相關標籤/搜索