凤翔网站建设溧水城乡建设局网站
PowerDesigner导入Excel模板生成数据表
1.准备好需要导入的Excel表结构数据,模板内容如下图所示

2.打开PowerDesigner,新建一个physical data model文件,填入文件名称,选择数据库类型

3.点击Tools|Execute Commands|Edit/Run Script菜单或按下快捷键Ctrl + Shift + X打开脚本窗口,输入示例VBScript脚本,修改其中的Excel模板路径及工作薄页签,点Run按钮执行即可

4.VBScript脚本
'******************************************************************************  
'* 所有的表设计都放在一个excel的一个sheet中,每个表中间空一行,表体都有表头说明如下,  
'* 再前面一行是表名和表的说明,分别在A和C列。下面格式直接拷贝到excel中就可以看到,空格是制表符。  
'******************************************************************************    
'                             Excel 格式如下 
'表/字段中文名	表/字段代码			表/字段注释		字段类型		是否主键	是否非空
'班级学生信息表	CLASS_INFO_TABLE	班级学生信息表			
'主键			ID					主键			varchar2(64)	是			是
'姓名			NAME				姓名			varchar2(255)	否			否
'年龄			AGE					年龄			varchar2(64)	否			否
'性别			SEX					性别			varchar2(64)	否			否
'班级名称		CLASS_NAME			班级名称			varchar2(64)	否			否
'学号			NO					学号			varchar2(64)	否			否
'分数			sorce				考试成绩			NUMBER(8,2)		否			否'******************************************************************************    
Option Explicit  Dim mdl ' the current model  
Set mdl = ActiveModel  
If (mdl Is Nothing) Then  MsgBox "There is no Active Model"  
End If  Dim HaveExcel  
Dim RQ  
RQ = vbYes 'MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")  
If RQ = vbYes Then  HaveExcel = True  ' Open & Create Excel Document  Dim x1  '  Set x1 = CreateObject("Excel.Application")  x1.Workbooks.Open "D:\my_study\示例文件\pdm导入Excle文件.xlsx"  '指定excel文档路径  x1.Workbooks(1).Worksheets("Sheet1").Activate   '指定要打开的sheet名称  
Else  HaveExcel = False  
End If  a x1, mdl  
sub a(x1, mdl)  
dim rwIndex  
dim tableName  
dim colname  
dim table  
dim col  
dim count  
dim abc  on error Resume Next  
'--------------------------------  
'下面是读取excel,添加表实体属性  
'--------------------------------  
For rwIndex = 2 To 1000  '指定要遍历的Excel行标  第一行是表头,非表字段信息部分,不读取,所以从第2行开始,表结构有多少行就读取多少行With x1.Workbooks(1).Worksheets("Sheet1")'需要循环的sheet名称  If .Cells(rwIndex, 1).Value = "" Then                            Exit For             End If If .Cells(rwIndex,4).Value = "" And  .Cells(rwIndex,5).Value = ""  Then  set table = mdl.Tables.CreateNew '创建一个表实体  table.Code = .Cells(rwIndex,2).Value'从excel中取得表名称和编码  table.Name = .Cells(rwIndex,1).Value'table.Comment = .Cells(rwIndex,3).Valuecount = count + 1  End If If .Cells(rwIndex,4).Value <>"" Thenset col =table.Columns.CreateNew '创建一列/字段  col.Name = .Cells(rwIndex, 1).Value '指定列name  col.Code = .Cells(rwIndex, 2).Value '指定列code  col.Comment =  .Cells(rwIndex, 3).Value '指定列说明 col.DataType = .Cells(rwIndex, 4).Value '指定列数据类型 			If .Cells(rwIndex, 5).Value = "true" or .Cells(rwIndex, 5).Value = "是" or .Cells(rwIndex, 5).Value = "True" Then'指定主键  col.Primary = true  End If       If .Cells(rwIndex, 6).Value = "true" or .Cells(rwIndex, 6).Value = "是" or .Cells(rwIndex, 6).Value = "True" Then'指定列是否可空 true 为不可空  col.Mandatory = true  End If End If  End With  
Next  MsgBox "生成数据表结构共计 " + CStr(count), vbOK + vbInformation, "表"  
Exit Sub  
End sub
 
[2024-06-08]
