'****************************************************************************** '* File: excel2pdm.txt '* Title: pdm export to excel '* Purpose: To export the tables and columns to Excel '* Model: Physical Data Model '* Objects: Table, Column, View '* Author: ziyan '* Created: 2012-05-03 '*Modifier: Hui Wanpeng 2014/07/04 '* Version: 1.0 '****************************************************************************** OptionExplicit Dim md1 'the current model Set md1=ActiveModel If(md1 IsNothing) Then MsgBox"There is no Active Model" EndIf 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 "E:/tmp/B瓚呮鏌ヨ〃.xls" x1.Workbooks(1).Worksheets("Sheet1").Activate Else HaveExcel=False EndIf process x1, md1 sub process(x1,md1) dim rwIndex dim tableName dim colname dim table dim col dim count dim dType dim nNull 'on error Resume Next For rwIndex =1To500 step 1 With x1.Workbooks(1).Worksheets("Sheet1") If .Cells(rwIndex,1).Value=""Then ExitFor EndIf If .Cells(rwIndex,3).Value=""Then set table=md1.Tables.CreateNew table.Name=.Cells(rwIndex,2).Value table.Code=UCase(.Cells(rwIndex,1).Value) table.Comment=.Cells(rwIndex,2).Value count=count+1 Else colName=.Cells(rwIndex,1).Value set col=table.Columns.CreateNew 'MsgBox.Cells(rwIndex,1).Value 'MsgBox colName,vbOK+vbInformation,"鍒? col.Code=Trim(UCase(.Cells(rwIndex,1).Value)) col.Name=Trim(UCase( .Cells(rwIndex,1).Value)) col.Comment=Trim(.Cells(rwIndex,2).Value) dType=Trim(UCase(.Cells(rwIndex,3).Value)) 'MsgBox Left(dType, 5) IfLeft(dType, 5)="CHAR("Then dType=Replace(dType,"CHAR","VARCHAR2") ElseIfLeft(dType, 5)="CAHR("Then dType=Replace(dType,"CAHR","VARCHAR2") EndIf col.DataType=dType nNull=Trim(UCase(.Cells(rwIndex,4).Value)) If nNull="NOT NULL"then col.Mandatory="true" EndIf EndIf EndWith Next MsgBox"鐢熸垚鏁版嵁琛ㄧ粨鏋勫叡璁?nbsp;"+CStr(count), vbOK+vbInformation, "琛?/span>" x1.Workbooks.Close ExitSub End Sub
VERSION 1.0Class BEGINCLASS BEGIN MultiUse =-1'True END Attribute VB_Name ="Sheet3" Attribute VB_GlobalNameSpace =False Attribute VB_Creatable =False Attribute VB_PredeclaredId =True Attribute VB_Exposed =True ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Rem 妯″潡鍚嶇О: 鐢熸垚鎻掑叆SQL Rem Rem 浣滆? Huyvanpull Rem Rem 鐗堟湰: V0.1 Rem Rem 緙栧啓鏃墮棿: 2011.09.16 Rem Rem 淇敼鏃墮棿: 2011.09.16 Rem Rem 鍔熻兘鎻忚堪: 鏍規嵁鏁版嵁Sheet鐨勫唴瀹瑰湪鍙︿竴涓猄heet鍐呯敓鎴愭彃鍏QL Rem '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' OptionExplicit Const strTableNameCell ="A1"'琛ㄥ悕鎵鍦ㄧ殑浣嶇疆 Const intHeaderRow =3'鏁版嵁琛ㄥご鎵鍦ㄨ Const strDataSheetName ="鏁版嵁婧?/span>"'淇濆瓨鏁版嵁鐨凷heet鍚嶇О Const strIsqlSheetName ="鎻掑叆SQL"'淇濆瓨SQL鐨凷heet鍚嶇О Const strDeleSheetName ="鍒犻櫎SQL"'瀛樺垹闄QL鐨凷heet鍚嶇О Dim strTableName AsString'鏁版嵁搴撹〃鍚?/span> Dim strTemSql AsString'涓存椂SQL璇彞 Dim strInsertSql AsString'鎻掑叆SQL璇彞 Dim intClumnCount AsInteger'鍒楁暟 Dim intIndex1 AsInteger'绱㈠紩鍙橀噺 Dim intIndex2 AsInteger'絎簩涓儲寮曞彉閲?/span> Dim intIndex3 AsInteger'絎笁涓彉閲?/span> Rem 嬋媧繪湰Sheet鏃舵墽琛?鐢熸垚鎻掑叆SQL PrivateSub Worksheet_Activate()Sub Worksheet_Activate() Rem 娓呯┖SQL鐨凷heet Worksheets(strIsqlSheetName).Select Cells.Select Selection.ClearContents ActiveCell.Select Rem 寰楀埌琛ㄥ悕 strTableName = Worksheets(strDataSheetName).Range(strTableNameCell).Value Rem 鍒楁暟 intClumnCount = Worksheets(strDataSheetName).Range("IV"& intHeaderRow).End(xlToLeft).Column Rem 寮濮嬬粍瑁匰QL璇彞 strTemSql ="INSERT INTO " strTemSql = strTemSql + strTableName strTemSql = strTemSql +" (" Rem 緇勮瀛楁澶?/span> For intIndex1 =1To intClumnCount strTemSql = strTemSql + Worksheets(strDataSheetName).Cells(intHeaderRow, intIndex1).Value If intIndex1 < intClumnCount Then strTemSql = strTemSql +"," EndIf Next intIndex1 Rem 涓嬫潯璇彞緇勮TempSQL瀹屾垚 strTemSql = strTemSql +") VALUES (" Rem 緇勮SQL璇彞浣?/span> For intIndex2 = intHeaderRow +1To Worksheets(strDataSheetName).UsedRange.Rows.Count strInsertSql = strTemSql For intIndex3 =1To intClumnCount Rem 鍔犱笂鍗曞厓鏍奸噷鐨勬暟鎹?/span> strInsertSql = strInsertSql + getCellVal(Worksheets(strDataSheetName).Cells(intIndex2, intIndex3)) If intIndex3 < intClumnCount Then strInsertSql = strInsertSql +"," EndIf Next intIndex3 strInsertSql = strInsertSql +");" Rem MsgBox strInsertSql Rem 鍚戞彃鍏QL鐨凷heet璧嬪?/span> Worksheets(strIsqlSheetName).Cells(intIndex2 - intHeaderRow, 1).Value = strInsertSql Next intIndex2 Rem 璁劇疆鎻掑叆SQL鐨凷heet鐨勬牱寮?/span> Worksheets(strIsqlSheetName).UsedRange.Select With Selection .Font.Size =9'璁劇疆瀛楀彿Font.Name = "MS Sans Serif" '璁劇疆瀛椾綋 .Font.Color =1'璁劇疆瀛楃殑棰滆壊Borders.LineStyle = xlContinuous '璁劇疆瀹炵嚎杈規 .Columns.AutoFit '璁劇疆鍗曞厓鏍煎搴﹁嚜閫傚簲錛堟牴鎹崟鍏冩牸鍐呮枃瀛楅兘鏄嚜鍔ㄨ皟鑺傝鍗曞厓鏍肩殑瀹藉害錛?/span> EndWith Rem 閫変腑絎竴涓崟鍏冩牸 Worksheets(strIsqlSheetName).Range("A1").Select Rem 鍒犻櫎SQL鐨凷heet鐨勫?/span> Worksheets(strDeleSheetName).Range("A1").Value ="--DELETE FROM "+ strTableName +" WHERE 1=1" Worksheets(strDeleSheetName).Range("A4").Value =" Write By: Huyvanpull" Worksheets(strDeleSheetName).Range("A5").Value =" QQ: 182429125" Worksheets(strDeleSheetName).Range("A6").Value =" Date: 2011-09-17" End Sub Rem 鏍規嵁綾誨瀷寰楀埌Cell閲岀殑鍊肩殑鍑芥暟 Function getCellVal()Function getCellVal(c) Dim tempStr AsString Rem 濡傛灉鍗曞厓鏍兼槸鏁板瓧 IfIsNumeric(c.Value) Then tempStr ="'" Rem 濡傛灉涓嶆槸鏁存暟,鍦ㄥ墠闈㈠姞0 IfInt(c.Value) <> c.Value Then tempStr = tempStr +"0" EndIf tempStr = tempStr +CStr(c.Value) tempStr = tempStr +"'" Rem 濡傛灉鍗曞厓鏍兼槸鏄棩鏈熷瀷 ElseIfIsDate(c.Value) Then tempStr ="to_date('" tempStr = tempStr +Format(c.Value, "yyyy-mm-dd hh:mm:ss") tempStr = tempStr +" ','yyyy-mm-dd hh:mi:ss')" Rem 濡傛灉鍗曞厓鏍兼槸鍏跺畠鏁版嵁綾誨瀷 Else tempStr ="'" tempStr = tempStr +CStr(c.Value) tempStr = tempStr +"'" EndIf Rem 榪斿洖瀛楃涓?/span> getCellVal = tempStr End Function