星空网 > 软件开发 > 数据库

PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型

本文使用的数据库类型是Oracle 11g

最近在工作中遇到一个问题:数据的设计以表格的形式保存在Excel文件中。(由于保密原因,我只能看到数据库设计文档,无法访问数据库。=_=!)

其中包括Name,Code,DataType,Unit,Length,Precision,Primary,Foreign Key,Mandatory,Comment等字段。

现在我要使用PowerDesigner重新建立这些表的物理模型,但是面对好几百个表,如果使用复制粘贴的方式不但费事费力,而且容易出错。

PowerDesigner提供了很多扩展功能,其中包括从Excel文件导入物理数据模型。

导入的方式有两种,一种是把表的设计模型整理为PowerDesigner要求的格式,另外一种是编写VBScript脚本。

由于编写VBScritp比较灵活,而且把表的设计整理为PowerDesigner需要的格式也比较麻烦,所以通过编写VBScript脚本导入Excel中的模型数据。

PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型
 1 '============================================================ 2 '从Excel文件中导入PowerDesigner 物理数据模型 3 ' 4 '注意:1,Excel表格中不能有合并的单元格 5 '   2,列之间不能有空行 6 '============================================================ 7  8  9 Option Explicit 10  11 '============================================================ 12 '私有全局变量。 13 '============================================================ 14 Private CURRENT_MODEL 15 Private TABLES 16 Private EXCEL_APP 17 Private FILE_PATH 18  19 Set CURRENT_MODEL = ActiveModel 20 Set EXCEL_APP = CreateObject("Excel.Application") 21 FILE_PATH="D:\models.xlsx"  '文件的绝对路径 22  23  24 '检查文件是否存在 25 If CheckFileExsistence() Then 26  '检查当前是否有已经打开的物理图 27  If CURRENT_MODEL is Nothing Then 28    MsgBox("请先打开一个物理数据模型(Physical Data Model),然后再进执行导入!") 29  Else 30    Set TABLES = CURRENT_MODEL.Tables 31    '根据EXCEL表格创建模型 32    ImportModels() 33  End If 34 Else 35  MsgBox "文件" + FILE_PATH + "不存在!" 36 End If 37  38  39 '============================================================ 40 '检查文件是否存在 41 '============================================================ 42 Function CheckFileExsistence 43  Dim fso 44  Set fso = CreateObject("Scripting.FileSystemObject") 45  CheckFileExsistence = fso.FileExists(FILE_PATH) 46 End Function 47  48  49 '============================================================ 50 '导入模型 51 '============================================================ 52 Sub ImportModels 53  '打开Excel文件 54   EXCEL_APP.Workbooks.Open FILE_PATH 55  56  Dim worksheets 57  Dim worksheetCount 58  Set worksheets = EXCEL_APP.Worksheets 59  worksheetCount = worksheets.Count 60  If worksheetCount <= 0 Then 61    Exit Sub 62  End If 63   64  Dim index 65  Dim currentSheet 66  For index = 1 to worksheetCount 67    Set currentSheet = worksheets(index) 68    CreateTable currentSheet 69  Next 70   71  '关闭Excel文件 72   EXCEL_APP.Workbooks.Close 73 End Sub 74  75  76 '============================================================ 77 '创建表 78 '============================================================ 79 Sub CreateTable(worksheet) 80  Dim cells 81  Set cells = worksheet.Cells 82  '检查具有相同名称的表是否已经存在 83  If TableExists(cells(1, 1).Value) Then 84    MsgBox "表“" + cells(1, 1).Value + "”已经存在!" 85    Exit Sub 86  End If 87   88  Dim table 89  Set table = TABLES.CreateNew 90  table.Name = cells(1, 1).Value 91  table.Code = cells(2, 1).Value 92  table.Comment = cells(3, 1).Value 93   94   95  Dim index 96  Dim rows 97  Dim col 98  Set rows = worksheet.Rows 99  For index = 4 to 512100    If EXCEL_APP.WorksheetFunction.CountA(rows(index)) <= 0 Then101     Exit For102    End If103  104    Set col = table.Columns.CreateNew105    col.Name = cells(index, 1).Value         '字段的中文含义106    col.Code = cells(index, 2).Value         '字段名107    col.Unit = cells(index, 4).Value         '字段的单位108    col.DataType = cells(index, 3).Value       '字段的数据类型109    col.Comment = cells(index, 8).Value        '字段的注释110  Next111 End Sub112 113 114 '============================================================115 '检查表是否已经存在116 '============================================================117 Function TableExists(tableName)118 Dim tbl119 For Each tbl in TABLES120  If StrComp(tbl.Name, tableName) = 0 Then121    TableExists = True122    Exit Function123  End If124 Next125  TableExists = False126 End Function

View Code

下面是测试脚本使用的Excel文件格式:

前三行的第一个单元格中的值分别是:表名称,数据库表名称,表的说明信息。

第4行到低7行都是列信息。

A到H列依次为:列名,字段名称,数据类型,单位,是否主键,是否外键,是否非空,列注释信息。

PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型

 

以下是Excel导入的步骤:

1,在PowerDesigner中先打开一个物理数据模型,然后按Ctrl + Shift + X执行脚本。

   执行完成之后的界面如下:

PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型

2,展开上图所示的Tables文件夹。左键点击图标,把表拖到指定的物理图(Physical Diagram)中。

这样就实现了从Excel中导入数据库物理模型。

PowerDesigner 16.5 使用VBScript脚本从Excel导入物理数据模型

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

儋州海运:https://www.goluckyvip.com/tag/99006.html
南沙出口海运:https://www.goluckyvip.com/tag/99007.html
青岛海运越南:https://www.goluckyvip.com/tag/99008.html
日本海运海运:https://www.goluckyvip.com/tag/99009.html
广西到山东海运:https://www.goluckyvip.com/tag/99010.html
茂名港海运:https://www.goluckyvip.com/tag/99011.html
TikTok 将推出先买后付服务 :https://www.goluckyvip.com/news/188219.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流