在Excel建立表。然后用PowerDesigner这个工具里用vb代码来读取这个excel表。然后在PowerDesigner这个工具中生成sql语句。然后放到数据中执行
特别要注意:excel中的两个表中间以一个空白行来分隔的。不能多,也不能少

1

2

3

4
然后再弹出框中输入一下代码就可以了。这样就可以将excel中建立的表生成SQL语句

提供具体的vb语句 (只要修改你excel做在的位置即可遍历的excel)【即修改这里:x1.Workbooks.Open "G:\项目介绍及资料\鼻咽癌单病种数据库管理数据结构.xlsx" '指定Excel表的路径"】
如果你的excel工作薄有多个sheet,那就只要将 【For i = 1 To 1 '指定要导入的sheet的个数 】 这个修改一下就可以了,比如你excel工作薄有3个sheet。那么就只要将For i=1 To 3 即可。
- Option Explicit '强制显示声明模块中的所有变量
- '如模块中使用Option Explicit则须用 Dim、Private、Public、ReDim 或 Static 语句来显 式声明所有的变量。
- 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 "G:\项目介绍及资料\鼻咽癌单病种数据库管理数据结构.xlsx" '指定Excel表的路径"
- ' x1.Workbooks(1).Worksheets("Sheet1").Activate '指定要打开的sheet名称
- Else
- HaveExcel = False
- End If
-
- ImportExcel x1, mdl
-
- '--------------------------------
- ' 导入函数
- '--------------------------------
- Sub ImportExcel(x1, mdl)
- Dim rwIndex '定义变量
- Dim tableName '定义变量
- Dim colname '定义变量
- Dim table '定义变量
- Dim col '定义变量
- Dim count '定义变量
- Dim i
-
- 'on error Resume Next
- For i = 1 To 1 '指定要导入的sheet的个数
- For rwIndex = 1 To 200 step 1 '指定要遍历的Excel行标
- With x1.Workbooks(1).Worksheets(i) '定义需要写入表结构所在的sheet
- 'MsgBox "生成数据表结构共计1 ="+CStr(.Cells(2,2).Value ), vbOK + vbInformation, "表"
-
- If .Cells(rwIndex, 1).Value <> "" Then
- If .Cells(rwIndex, 1).Value = "表中文名" Then '表中文名
- Set table = mdl.Tables.CreateNew '创建一个实体表
- table.Name = .Cells(rwIndex , 2).Value
- count = count + 1
- ElseIf .Cells(rwIndex, 1).Value = "表英文名" Then '表英文名
- table.Code = .Cells(rwIndex , 2).Value
- ElseIf .Cells(rwIndex, 1).Value = "表描述" Then '表描述
- table.Comment = .Cells(rwIndex , 2).Value
- ElseIf .Cells(rwIndex, 1).Value <> "字段中文名" Then
- Set col = table.Columns.CreateNew '创建一列/字段
- col.Name = .Cells(rwIndex, 1).Value '指定字段名
- col.Code = .Cells(rwIndex, 2).Value '指定字段名
- col.DataType = .Cells(rwIndex, 3).Value '指定列数据类型
- col.Comment = .Cells(rwIndex,4).Value '指定列说明
- ' col.DefaultValue = .Cells(rwIndex, 6).Value '指定列数据类型
- '设置主键
- If.Cells(rwIndex, 5).Value = "主键" Then
- col.Primary = True
- End If
- '设置为空值
- If.Cells(rwIndex, 5).Value = "非空" Then
- col.Mandatory = True
- End If
- End If
-
- End If
-
- End With
- Next
- Next
-
- x1.Workbooks(1).Close
-
- MsgBox "生成数据表结构共计 " + CStr(count), vbOK + vbInformation, "表"
-
- Exit Sub
- End Sub
执行完这段代码后,后在左边生成一个Table,然后可以将里面的表拖到右边的框中,就可以看到表结构视图了。


|