一、模板规范
T_STUDENT | 学生表 | ||
---|---|---|---|
ID | ID | VARCHAR(255) | 主键 |
姓名 | NAME | VARCHAR(255) | |
性别 | SEX | CHAR(1) | 0-男;1-女 |
年龄 | AGE | VARCHAR(255) |
Excel中模板如下:
二、脚本
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:\DEMO\表模板.xls"
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
Exit For
End If
If .Cells(rwIndex, 3).Value = "" Then
set table = mdl.Tables.CreateNew
table.Code = .Cells(rwIndex , 1).Value
table.Name = .Cells(rwIndex , 2).Value
table.Comment = .Cells(rwIndex , 4).Value
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,4).Value
col.DataType = .Cells(rwIndex, 3).Value
End If
End With
Next
MsgBox "生成数据表结构共计" + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub
为了方便使用,推荐把以上代码保存为".vbs"的文本文档
其中第16行模板位置及命名需根据实际情况调整(注意区分Excel文档格式中的".xlsx"和".xls")
第17行和第34行保持一致且与Excel的工作表命名对应
三、生成表结构
1、打开powerdesigner,点击Create model
2、如图所示,从左到右依次选中,点击OK
3、快捷键Ctrl+shift+x,把代码脚本粘上去就行。或者打开刚才新建为".vbs"的脚本
4、点击run,即可生成。
5、此时表结构就已经生成了,但在当前视图中没有此表,选中刚才生成的表结构拖拽到视图中。