powerDesigner 从数据库导出表结构,并生产excel ,有效

时间:2022-04-15 05:38:42

将数据库中的表导入到PowerDesigner中并转为excel文档

1、打开PowerDesigner12,在菜单中按照如下方式进行操作

   file->Reverse Engineer->DataBase

   点击后,弹出 New Physical Data Model 的对话框

2、在General选项卡中

    Model name:模板名字,自己命名。

    DMBMS    :根据需要选择,我选择的是ORACLEVersion 10g

点确定后弹出 Database Reverse Engineering Option 对话框

3  Selection选项卡中,选中Using a data source选项

   注意如果是第一次导入数据,需要你先自己配制ODBC数据源,方法如下:

1)点击 下面 右侧的数据库 连接按钮,弹出 Connect to a Data Source 的对话框

 

       2)选中 ODBC machine data source 选项,点击 Modify按钮右边的 Configure 按钮   弹出Configure Data Connections

3)在 ODBC Machine Data Sources选项卡   下面的工具栏中点击 第二个 数据库 配置按钮(Add Data Source(Ctrl+N)) 弹出创建新数据源窗口 ,选择文件数据源,    选择相应的数据源驱动程序(我选Oracle),点下一步(Modify),给数据源起个名字,选择服务器,下一步,输入相应的密码 

配置完毕 后 点击确定

 

4,选择刚刚配置的数据源,输入相应的用户名和密码   即可。 、

选择需要导入的表

 

 

powerDesigner生成excel版本的数据库文件

 

PowerDesigner中,shift+ctrl+X,打开脚本运行,脚本如下:

'******************************************************************************

 

'* 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 activemodel

 

Dim Model

 

Set Model = ActiveModel

 

If (Model Is Nothing) Or (NotModel.IsKindOf(PdPDM.cls_Model)) Then

 

  MsgBox "The current model is not an PDMmodel."

 

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(7).ColumnWidth = 10

 

 sheet.Columns(8).ColumnWidth = 10

 

 sheet.Columns(9).ColumnWidth = 10

 

 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, 9)).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.cells(rowsNum, 7) = "是否主键"

     

      sheet.cells(rowsNum, 8) = "是否外键"

     

      sheet.cells(rowsNum, 9) = "是否必填"

 

      '设置边框

 

      sheet.Range(sheet.cells(rowsNum-1,1),sheet.cells(rowsNum, 2)).Borders.LineStyle = "1"

 

      sheet.Range(sheet.cells(rowsNum-1,4),sheet.cells(rowsNum, 9)).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

     

      sheet.cells(rowsNum, 7) = col.primary

     

      sheet.cells(rowsNum, 8) = col.foreignkey

     

      sheet.cells(rowsNum, 9) = col.mandatory

 

      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,9)).Borders.LineStyle= "2"

 

      rowsNum = rowsNum + 1

 

     

 

      Output "FullDescription: "       + tab.Name

 

   End If

 

End Sub