浪费别人的时间等于是谋财害命,浪费自己的时间等于是慢性自杀。 —— 列宁

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导入模板

posted @ 2019-03-06 17:25  一谦的视界  阅读(2660)  评论(1编辑  收藏