设计表的时候会用到powerdesigner,但是有时候我们喜欢在Excel中把表字段列举出来后再使用powerdesigner,于是就需要把Excel导入到powerdesigner中。

准备好Excel中表内容,并保存到d盘,命名为t_hospital.xslx(路径在脚本中会体现)

powerdesigner和Excel交互

打开powerdesigner,创建物理模型(Physical Data Model)

powerdesigner和Excel交互

点击PowerDesigner菜单栏“Tools ->Excute Commands->Edit/Run Script..”

powerdesigner和Excel交互

输入vb脚本

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:\t_hospital.xlsx"
  x1.Workbooks(1).Worksheets("Sheet1").Activate
Else
   HaveExcel = False
End If
 
a x1, mdl
 
sub a(x1,mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim count
 
'on error Resume Next
For rwIndex = 1 To 1000 step 1   
    With x1.Workbooks(1).Worksheets("Sheet1")
  'MsgBox "生成数据表结构共计1 ="+CStr(.Cells(2,2).Value ), vbOK + vbInformation, "表"
   If .Cells(rwIndex, 1).Value = "" Then
       rwIndex = rwIndex + 1
       IF .Cells(rwIndex, 1).Value = "" then
       Exit For
       end if
   End If  
  If  .Cells(rwIndex, 3).Value = "" Then
    set table = mdl.Tables.CreateNew
        table.Name = .Cells(rwIndex , 1).Value
        table.Code = .Cells(rwIndex , 2).Value
        rwIndex  = rwIndex + 1
        count = count + 1  
   Else   
    colName = .Cells(rwIndex, 1).Value
    set col = table.Columns.CreateNew  
   'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列"
    col.Name = .Cells(rwIndex, 1).Value
    'MsgBox col.Name, vbOK + vbInformation, "列"
    col.Code = .Cells(rwIndex, 2).Value
    col.Comment = .Cells(rwIndex,1).Value  
    col.DataType = .Cells(rwIndex, 3).Value
    
   End If
  End With
Next
 
MsgBox "生成数据表结构共计" + CStr(count), vbOK + vbInformation, "表"
 
Exit Sub
End sub

之后点击run

powerdesigner和Excel交互

可以看到左侧表已经生成了

powerdesigner和Excel交互

执行完脚本在close的时候会提示是否保存

powerdesigner和Excel交互

可选择“是”,然后将脚本保存起来下回继续使用,我此处保存为“importExcel.vbs”

powerdesigner和Excel交互

存在问题:用Excel表格和vb脚本导入powerdesigner生成的table没有symbol模型,就是在物理模型页面中没有table模型展示

目前我的解决方法:将生成的table找到sql脚本,保存为“表.sql”文件,执行PowerDesigner菜单“File-->reverse engineer-->Database”

powerdesigner和Excel交互

 

然后弹出框点确定,进入下一步

powerdesigner和Excel交互

选择Using script files,然后选择保存的sql文件,点击确定,便生成了带有symbol的table,然后把没有symbol的table删除了就可以。

步骤相对比较冗余,没找到一步到位的方法。

 

 

 

 

 

 

 

 

 

相关文章:

  • 2021-12-29
  • 2021-05-23
  • 2021-11-02
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
相关资源
相似解决方案