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 "D:/Test.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 i
'on error Resume Next
For i= 1 To x1.Workbooks(1).Sheets.Count '指定要导入的sheet的个数
For rwIndex = 2 To 100 step 1 '指定要遍历的Excel行标
If x1.Workbooks(1).Worksheets(i) Is Nothing Then
Exit For
Else
With x1.Workbooks(1).Worksheets(i) '定义需要写入表结构所在的sheet
'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.Name = .Cells(rwIndex , 1).Value
table.Code = .Cells(rwIndex , 2).Value
'table.DefaultValue = .cells(rwIndex,6).value
count = count + 1
ElseIf rwIndex = 3 then
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 '指定列数据类型
' col.DefaultValue = .Cells(rwIndex, 6).Value '指定列数据类型
'设置主键
if.Cells(rwIndex, 4).Value = "是" Then
col.Primary = true
End If
'设置为空值
If.Cells(rwIndex, 5).Value = "否" Then
col.Mandatory = true
End If
End If
End With
End If
Next
Next
MsgBox "生成数据表结构共计 " + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub