目录
1,利用pdm生成excel
2,利用excel生成pdm(一个sheet是一个表)
3,通过pdm导出全部建表sql
1,利用pdm生成excel
一:使用pd(PowerDesigner)打开pdm,然后点击 ctrl+shift+x
二:在出现的代码框中输入下面代码,效果展示如下
'******************************************************************************
Option Explicit
Dim rowsNum,tablesNum
rowsNum = 0
tablesNum = 0
'-----------------------------------------------------------------------------
' Main function,excel 表的顺序是按照pdm中建立的前后顺序来生成的
'-----------------------------------------------------------------------------
' 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=2
' 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
'使sheet分等级AB
'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,sheetName
'EXCEL.workbooks.add(-4167)'添加工作表
BOOK.Sheets.Add , BOOK.Sheets(BOOK.Sheets.count)
'sheet的名字命名
sheetName = tab.code+"("+tab.name+")"
BOOK.Sheets(BOOK.Sheets.count).Name = sheetName
set SHEET = BOOK.sheets(sheetName)
'EXCEL.workbooks(1).sheets.add(-4167)
EXCEL.workbooks(1).Sheets(tablesNum).Select
EXCEL.visible = true
'设置列宽
sheet.Columns(1).ColumnWidth = 5
sheet.Columns(2).ColumnWidth = 5
sheet.Columns(3).ColumnWidth = 5
sheet.Columns(4).ColumnWidth = 25
sheet.Columns(5).ColumnWidth = 25
sheet.Columns(6).ColumnWidth = 25
sheet.Columns(7).ColumnWidth = 5
sheet.Columns(8).ColumnWidth = 25
'不显示网格线
EXCEL.ActiveWindow.DisplayGridlines = true
rowsNum = 0
rowsNum = rowsNum + 1
' Show properties
Output "================================"
sheet.cells(rowsNum, 1) ="返回目录"
sheet.cells(rowsNum, 4) =tab.name
sheet.cells(rowsNum, 5) = tab.code
sheet.cells(rowsNum, 6) = tab.comment
'sheet.cells(rowsNum, 6) = ""
'sheet.cells(rowsNum, 7) = "表说明"
'sheet.cells(rowsNum, 8).HorizontalAlignment=3
'下面代码是合并单元格
'sheet.Range(sheet.cells(rowsNum, 4),sheet.cells(rowsNum, 7)).Merge
'设置超链接,从目录点击表名去查看表结构 注意:首页的超链接在这里设置的
'(rowIndex,2)指的是你要设置超链接的位置,(sheetName&"!A"&rowsNum)指的是你要连接到的位置
'注意下面两行相互对应
sheetList.Hyperlinks.Add sheetList.cells(rowIndex,2), "","'"&sheetName&"'"&"!A"&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.cells(rowsNum, 8) = "備註"
'为标题列添加颜色
sheet.Range(sheet.cells(rowsNum, 1),sheet.cells(rowsNum, 8)).Interior.Color = RGB(254,127,127)
Dim col ' running column
Dim colsNum
colsNum = 0
for each col in tab.columns
rowsNum = rowsNum + 1
colsNum = colsNum + 1
sheet.cells(rowsNum, 1) = rowsNum-2'序号
sheet.cells(rowsNum, 5) = col.name'字段描述
sheet.cells(rowsNum, 4) = col.code'字段名
sheet.cells(rowsNum, 6) = col.datatype'数据类型
sheet.cells(rowsNum, 8) = col.comment'备注
If col.Primary = true Then'是否主键
sheet.cells(rowsNum, 2) = "Y"
Else
sheet.cells(rowsNum, 2) = ""
End If
If col.Mandatory = true Then'是否为空
sheet.cells(rowsNum, 7) = "N"
Else
sheet.cells(rowsNum, 7) = ""
End If
'sheet.cells(rowsNum, 7) = col.defaultvalue'默认值
next
'1代表实线,3代表虚线
'设置边框
sheet.Range(sheet.cells(1, 1),sheet.cells(rowsNum, 8)).Borders.LineStyle = "1"
'字体为微软雅黑
sheet.Range(sheet.cells(1, 1),sheet.cells(rowsNum, 8)).Font.Name="微软雅黑"
'字号为12号
sheet.Range(sheet.cells(1, 1),sheet.cells(rowsNum, 8)).Font.Size=12
'设置居中显示
sheet.Range(sheet.cells(1, 1),sheet.cells(rowsNum, 8)).HorizontalAlignment=3
'自动换行
sheet.Range(sheet.cells(1, 1),sheet.cells(rowsNum, 8)).WrapText=true
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
'添加pd mode名字
'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
'实线
SheetList.Range(SheetList.cells(1,1),SheetList.cells(rowsNo,4)).Borders.LineStyle = "1"
'字体为微软雅黑
SheetList.Range(SheetList.cells(1, 1),SheetList.cells(rowsNo, 4)).Font.Name="microsoft jhenghei"
'字号为12号
SheetList.Range(SheetList.cells(1, 1),SheetList.cells(rowsNo, 4)).Font.Size=12
'设置居中显示
SheetList.Range(SheetList.cells(1, 1),SheetList.cells(rowsNo, 4)).HorizontalAlignment=3
'自动换行
SheetList.Range(SheetList.cells(1, 1),SheetList.cells(rowsNo, 4)).WrapText=true
output "end"
End Sub
2,利用excel生成pdm(一个sheet是一个表)
默认model名字是Excel如需要可以修改,代码中已经做了注释
excel名字随意,第二行像column这些都可以随意取值,但是顺序不能错,分别是code,name,data type,主键,是否可以为空,comment(注释)
下面是excel的格式,也可以下载我上传的这个excel模板
'============================================================
'从Excel文件中导入PowerDesigner 物理数据模型
'
'注意:1,Excel表格中不能有合并的单元格
' 2,列之间不能有空行
'============================================================
Option Explicit
'============================================================
'私有全局变量。
'============================================================
Private CURRENT_MODEL_NAME
Private CURRENT_MODEL
Private TABLES
Private EXCEL_APP
Private FILE_PATH
'这里请输入model名字,默认是Excel注意大小写
CURRENT_MODEL_NAME = "Excel"
Set EXCEL_APP = CreateObject("Excel.Application")
FILE_PATH="C:\Users\admin\Desktop\模板.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(1, 2).Value
table.Comment = cells(1, 3).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 = "N" 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
3,通过pdm导出全部建表sql
首先打开使用PowerDesigner打开pdm,然后Ctrl+A全选,然后Ctrl+G,ok