Excel批量導入表結構到power designer

 1. 編寫測試EXCEL,格式如圖所示:








2. 修改腳本,指定excel所在路徑及文件名

3. 打開PowerDesigner,創建物理模型(Physical Data Model)

4. 在PowerDesigner菜單欄中,依次點擊“Tools ->Excute Commands->Edit/Run Script..

'******************************************************************************  
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:\111.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 = 1 To 253	'指定要遍歷的Excel行標  由於第2行是表頭,從第1行開始,看你這個表設計多少行
	With x1.Workbooks(1).Worksheets("Sheet1")'需要循環的sheet名稱
		If .Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "" Then'Excel中表頭的1列是表名,2空,3是表註釋
			set table = mdl.Tables.CreateNew '創建一個表實體
			table.Code = .Cells(rwIndex,1).Value'從excel中取得表名稱和編碼
			table.Name = .Cells(rwIndex,3).Value'
			table.Comment = .Cells(rwIndex,3).Value  '指定列說明
			count = count + 1
			Continue
		End If
		'If (.Cells(rwIndex,1).Value = "" And .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value = "") Or (.Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "")Then
      If .Cells(rwIndex,2).Value = "" or .Cells(rwIndex,1).Value = "字段" Then '第二列爲空的都可以忽略
			continue	'這裏忽略空行和表名行、表頭行
		Else 
			set col =table.Columns.CreateNew '創建一列/字段
         col.Code = .Cells(rwIndex, 1).Value	'指定列code
         col.DataType = .Cells(rwIndex, 2).Value	'指定列數據類型
         If.Cells(rwIndex, 3).Value = "主鍵" Then'指定主鍵
				col.Primary =true
			End If		
			If.Cells(rwIndex, 4).Value = "N" Then'指定列是否可空 true 爲不可空
				col.Mandatory =true
			End If
			col.Name = .Cells(rwIndex, 5).Value	'指定列name			
			col.Comment = .Cells(rwIndex, 6).Value  '指定列說明
		End If
	End With
Next
	MsgBox "生成數據表結構共計 " + CStr(count), vbOK + vbInformation, "表"
Exit Sub
End sub

5. 以上方式一次只能加一張表,下面提供批量加表方法(指定excel所在路徑及文件名, 列出sheet名):

 
'******************************************************************************  


Option Explicit
dim shet
dim cnt
cnt = 0
For each shet in array("s1","s2","s3","s4","s5","s6")  '列出excel文件所有sheet名稱,如s1, s2, s3~~~~~~
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:\app\Linens.xls"	'指定excel文檔路徑
	x1.Workbooks(1).Worksheets(shet).Activate	'指定要打開的sheet名稱
Else
	HaveExcel = False
End If

a x1, mdl
cnt = cnt + 1
Next
MsgBox "生成數據表結構共計 " + CStr(cnt), vbOK + vbInformation, "表"

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 = 1 To 253	'指定要遍歷的Excel行標  由於第2行是表頭,從第1行開始,看你這個表設計多少行
	With x1.Workbooks(1).Worksheets(shet)'需要循環的sheet名稱
		If .Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "" Then'Excel中表頭的1列是表名,2空,3是表註釋
			set table = mdl.Tables.CreateNew '創建一個表實體
			table.Code = .Cells(rwIndex,1).Value'從excel中取得表名稱和編碼
			table.Name = .Cells(rwIndex,3).Value'
			table.Comment = .Cells(rwIndex,3).Value  '指定列說明
			count = count + 1
			Continue
		End If
		'If (.Cells(rwIndex,1).Value = "" And .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value = "") Or (.Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "")Then
      If .Cells(rwIndex,2).Value = "" or .Cells(rwIndex,1).Value = "字段" Then '第二列爲空的都可以忽略
			continue	'這裏忽略空行和表名行、表頭行
		Else 
			set col =table.Columns.CreateNew '創建一列/字段
         col.Code = .Cells(rwIndex, 1).Value	'指定列code
         col.DataType = .Cells(rwIndex, 2).Value	'指定列數據類型
         If.Cells(rwIndex, 3).Value = "主鍵" Then'指定主鍵
				col.Primary =true
			End If		
			If.Cells(rwIndex, 4).Value = "N" Then'指定列是否可空 true 爲不可空
				col.Mandatory =true
			End If
			col.Name = .Cells(rwIndex, 5).Value	'指定列name			
			col.Comment = .Cells(rwIndex, 6).Value  '指定列說明
		End If
	End With
Next
	'MsgBox "生成數據表結構共計 " + CStr(count), vbOK + vbInformation, "表"

Exit Sub
End sub

或者

 
'******************************************************************************  


Option Explicit
dim shet
dim cnt
dim ind
cnt = 0
For ind = 1 TO 28
   shet = "s" + CStr(ind)  '拼接sheet名稱:s + 1/2/3/4/5/6/~~~
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:\app\Linens.xls"	'指定excel文檔路徑
	x1.Workbooks(1).Worksheets(shet).Activate	'指定要打開的sheet名稱
Else
	HaveExcel = False
End If

a x1, mdl
cnt = cnt + 1
Next
MsgBox "生成數據表結構共計 " + CStr(cnt), vbOK + vbInformation, "表"


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 = 1 To 253	'指定要遍歷的Excel行標  由於第2行是表頭,從第1行開始,看你這個表設計多少行
	With x1.Workbooks(1).Worksheets(shet)'需要循環的sheet名稱
		If .Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "" Then'Excel中表頭的1列是表名,2空,3是表註釋
			set table = mdl.Tables.CreateNew '創建一個表實體
			table.Code = .Cells(rwIndex,1).Value'從excel中取得表名稱和編碼
			table.Name = .Cells(rwIndex,3).Value'
			table.Comment = .Cells(rwIndex,3).Value  '指定列說明
			count = count + 1
			Continue
		End If
		'If (.Cells(rwIndex,1).Value = "" And .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value = "") Or (.Cells(rwIndex,1).Value <> "" And  .Cells(rwIndex,2).Value = "" And .Cells(rwIndex,3).Value <> "")Then
      If .Cells(rwIndex,2).Value = "" or .Cells(rwIndex,1).Value = "字段" Then '第二列爲空的都可以忽略
			continue	'這裏忽略空行和表名行、表頭行
		Else 
			set col =table.Columns.CreateNew '創建一列/字段
         col.Code = .Cells(rwIndex, 1).Value	'指定列code
         col.DataType = .Cells(rwIndex, 2).Value	'指定列數據類型
         If.Cells(rwIndex, 3).Value = "主鍵" Then'指定主鍵
				col.Primary =true
			End If		
			If.Cells(rwIndex, 4).Value = "N" Then'指定列是否可空 true 爲不可空
				col.Mandatory =true
			End If
			col.Name = .Cells(rwIndex, 5).Value	'指定列name			
			col.Comment = .Cells(rwIndex, 6).Value  '指定列說明
		End If
	End With
Next
	'MsgBox "生成數據表結構共計 " + CStr(count), vbOK + vbInformation, "表"

Exit Sub
End sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章