一、創建excel表
本示例中創建了三張sheet表分別是t_user、t_role、t_user_role三張表(sheet表可以建多個,表中數據的格式其實是可以自定義的,此處不做闡述)。如圖所示:
二、創建新的物理數據模型
1、打開PowerDesigner,點擊【File】==》【New Model】==》【Model types】==》【Physical Data Model】==》【Physical Diagram】==》【OK】。如圖所示:
Ps:*代表未保存項目,創建后【CTRL+S】可能是個好習慣。
2、然后點擊【Tools】==》【Execute Commands】==》【Edit/Run Script】編輯運行相關腳本。如圖所示:
輸入以下腳本內容,點擊【Run】執行腳本,如圖所示:
Option Explicit
dim shet
dim cnt cnt = 0 For each shet in array("t_user","t_role","t_user_role") '列出excel文件中所用需要讀取的sheet的名稱,例如:t_user、t_role、t_user_role... Dim mdl ' 當前的模型 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 "excel文檔路徑" '指定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 rwIndexdim table dim col on error Resume Next '-------------------------------- '讀取excel中的sheet表,並在model中創建物理表 '-------------------------------- For rwIndex = 1 To 10 '指定遍歷開始行-遍歷的行數 With x1.Workbooks(1).Worksheets(shet)'需要循環的sheet名稱 If rwIndex = 1 Then'sheet表中的第一行第一列為表中文名,第一行第二列為英文名,第一行第三列為表注釋信息 set table = mdl.Tables.CreateNew '創建一個表實體 table.Code = .Cells(rwIndex,2).Value'獲取表英文名,即表名 table.Name = .Cells(rwIndex,1).Value'獲取表中文名 table.Comment = .Cells(rwIndex,3).Value '獲取表注釋信息 Continue End If If rwIndex = 1 or rwIndex = 2 or .Cells(rwIndex,1).Value = "" Then '忽略第一行、第二行和空行(第一行為表相關信息,第二行為表頭信息) continue Else set col =table.Columns.CreateNew '創建一個字段實體 col.Code = .Cells(rwIndex, 1).Value '指定model中code在sheet表中對應的列 col.DataType = .Cells(rwIndex, 3).Value '指定model中的DataType在sheet表中對應的列 col.Name = .Cells(rwIndex, 2).Value '指定model中的Name在sheet表中對應的列 col.Comment = .Cells(rwIndex, 5).Value '指定model中的Comment在sheet表中對應的列 End If End With Next Exit Sub End sub
3、導出SQL語句,並在數據庫中建表。工具欄中點擊【Database】==》【Generate Database】==》【確定】保存SQL文件。如圖所示:
4、選擇一個數據庫,【表】右鍵==》【運行SQL文件】==》【選擇文件】==》【開始】執行SQL文件創建表。如圖所示: