PowerDesigner导入Excel模板生成数据表

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).Value
            count = count + 1  
        End If 
        If .Cells(rwIndex,4).Value <>"" Then
            set 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]

相关推荐

  1. 使用Excel导入导出数据

    2024-06-09 13:44:03       23 阅读
  2. Excel百万数据如何导入导出

    2024-06-09 13:44:03       19 阅读
  3. Excel表格导入/导出数据工具类

    2024-06-09 13:44:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-09 13:44:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 13:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 13:44:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 13:44:03       20 阅读

热门阅读

  1. common.js和es6中模块引入的区别

    2024-06-09 13:44:03       10 阅读
  2. web前端的MySQL:跨领域之旅的探索与困惑

    2024-06-09 13:44:03       15 阅读
  3. 机器学习学习

    2024-06-09 13:44:03       10 阅读
  4. 图搜索算法A*、Dijkstra在路径规划中的应用

    2024-06-09 13:44:03       10 阅读
  5. Lambda 表达式(C++11)

    2024-06-09 13:44:03       6 阅读
  6. 我的创作纪念日

    2024-06-09 13:44:03       8 阅读
  7. Vue中 .passive 修饰符的作用

    2024-06-09 13:44:03       10 阅读
  8. 【python】flask相关包依赖关系问题

    2024-06-09 13:44:03       9 阅读
  9. HTTP常见响应状态码

    2024-06-09 13:44:03       9 阅读