1. 程式人生 > >powerdesigner的匯入excel外掛

powerdesigner的匯入excel外掛

外掛的code是參考網上程式碼的(抄過來改了一兩行)

'匯入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:/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  

'on error Resume Next
'set table = mdl.Tables.CreateNew '建立一個表實體
'table.Name = "xxx表" '指定表名,如果在Excel文件裡有,也可以通過.Cells(rwIndex, 1).Value指定
'table.Code = "xxx"   '指定表Code,如果在Excel文件裡有,也可以通過.Cells(rwIndex, 2).Value指定
'count = count + 1
For rwIndex = 2 To 200 step 1 '指定要遍歷的Excel行標
        With x1.Workbooks(1).Worksheets("Sheet1")             
            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                
               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 '指定列code
                                    
               col.Comment = .Cells(rwIndex,1).Value '指定列說明    
                           
               col.DataType = .Cells(rwIndex, 3).Value '指定列資料型別
               
               If .Cells(rwIndex, 4).Value = "否" Then
                  col.Mandatory = true'指定列是否可空,true為不可空                  
               End If
               
            End If        
        End With 
Next

MsgBox "生成資料表結構共計" + CStr(count), vbOK + vbInformation, "張表"  

Exit Sub 
End Sub

可以把表結構整理成如下的excel表格,命名成test.xlsx,放在d盤根目錄,然後在PD上建立物理模型,執行上面的指令碼匯入即可。

下面的表格:

1. 第一行是各列介紹,不會被匯入

2. 黃色的兩格,分別是實體名和物理表名

3. 必須填充的是欄位名、欄位code、欄位型別

4. 其他列可自行增加,目的是為了excel更可讀

欄位名 欄位code 欄位型別 欄位處理描述 類目 識別符號 資料元名稱 說明
測試表 testtable
整形列 col_int int 測試
字元型列 col_char char(10) 測試
浮點型列 col_double double 測試
日期列 col_date date 測試
時間列 col_datetime datetime 測試
二進位制列 col_binary binary(1024) 測試
登記資訊 登記資訊
姓名 姓名 char(30) 二期高價值
曾用名 曾用名 char(30) 二期高價值
性別 性別 char(2) 二期高價值
民族 民族 char(2) 二期高價值
身份證件型別 身份證件型別 char(2) 二期高價值
身份證號碼 身份證號碼 char(18) 二期高價值
政治面貌 政治面貌 char(2)
出生地 出生地 char(50)
出生日期 出生日期 date 二期高價值
國籍 國籍 char(10)
照片 照片 binary(1024)
宗教信仰 宗教信仰 char(10)
兵役狀況 兵役狀況 char(2)
健康狀況 健康狀況 char(2) 二期高價值