1. 程式人生 > 其它 >PowerDesigner匯入Excel模板生成實體-多sheet頁支援

PowerDesigner匯入Excel模板生成實體-多sheet頁支援

技術標籤:powerdesigner資料庫

在Excel裡整理好的表模型資料,可直接匯入PowerDesigner。

此功能通過PowerDesigner的指令碼功能來實現,使用起來也簡單。具體操作方法:

開啟PowerDesigner,新建模型,點選Tools|Execute Commands|Edit/Run Script選單或按下快捷鍵Ctrl + Shift + X開啟指令碼視窗,輸入示例VBScript指令碼,修改其中的Excel模板路徑及工作薄頁籤,點Run按鈕執行即可。

本文提供指令碼支援EXCEL格式如下:

支援多sheet匯入

在這裡插入圖片描述
在這裡插入圖片描述

示例VBScript指令碼如下:

'匯入Excel表結構
'開始 Option Explicit Dim mdl ' the current model Set mdl = ActiveModel dim sheet If (mdl Is Nothing) Then MsgBox "There is no Active Model" End If Dim HaveExcel Dim RQ RQ = vbYes If RQ = vbYes Then HaveExcel = True ' Open & Create Excel Document Dim x1 ' Set x1 = CreateObject(
"Excel.Application") x1.Workbooks.Open "C:\Users\lily\Desktop\1.xlsx" '指定excel文件路徑 ' x1.Workbooks(1).Worksheets(sheet).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 Sht '迴圈所有sheet頁
For Each Sht In x1.Worksheets If Sht.Name = "sheet1" Then '第一頁為目錄 按需特殊處理 Else '逐步 遍歷指定行數 For rwIndex = 1 To 25 step 1 '指定要遍歷的Excel行標 With Sht If rwIndex = 2 Then '第二行為欄位名 特殊處理 Else '第一行為表 中文名 CODE If rwIndex =1 Then '指定表名 set table = mdl.Tables.CreateNew table.Name = .Cells(rwIndex , 1).Value table.Code = .Cells(rwIndex , 2).Value Else If .Cells(rwIndex, 1) =""Then '跳過沒有中文列名欄位 Else set col = table.Columns.CreateNew '建立列 If .Cells(rwIndex, 1).Value <>"" Then col.Name = .Cells(rwIndex, 1).Value '指定列名 End If If .Cells(rwIndex, 1).Value ="id" Then col.Primary = true '指定主鍵 End If If .Cells(rwIndex, 2).Value <>"" Then col.Code = .Cells(rwIndex, 2).Value '指定列code End If If .Cells(rwIndex, 6).Value <>"" Then col.Comment = .Cells(rwIndex,6).Value '指定列說明 End If If .Cells(rwIndex, 3).Value <>"" Then col.DataType = .Cells(rwIndex, 3).Value '指定列資料型別 End If If .Cells(rwIndex, 7).Va lue = "1" Then col.Mandatory = true '指定列是否可空,true為不可空 End If End If End If End If End With Next End If Next Exit Sub End Sub

借鑑 https://www.cnblogs.com/hulianfei/p/7122450.html 修改了多sheet頁的指令碼