把excel中的數(shù)據(jù)導(dǎo)入SQL SERVER數(shù)據(jù)庫(access數(shù)據(jù)類似): Private Sub Command1_Click() Dim strconn As String ' 定義Excel 連接字符串 Dim cn As ADODB.Connection ' 定義Excel 連接 Set cn = New ADODB.Connection ' 初始化commandialog1 的屬性,選取Excel 文件,文 ' 件名保存在CommanDialog1.filename 中備用
CommonDialog1.Filter = " 電子表格文件(.xls) |*.xls" CommonDialog1.DialogTitle = " 請選擇要導(dǎo)入的文件" CommonDialog1.ShowOpen
' 設(shè)置連接SQL 數(shù)據(jù)庫的連接字符串 strtemp = " [odbc;Driver= {SQL Server} ;Server=(local);Database=Afws;UID=sa;PWD=sa]" ' 設(shè)置Excel 數(shù)據(jù)連接 strconn = " Provider =Microsoft.Jet.OLEDB.4.0;Data Source=" & CommonDialog1.FileName & " ; Extended Properties=Excel 8.0" cn.Open strconn
strSql = "insert into " & strtemp & ".hw_level1 select * from [sheet1$]" cn.Execute strSql, lngRecsAff, adExecuteNoRecords
MsgBox " 成功導(dǎo)入到SQL 數(shù)據(jù)庫中,!", vbExclamation + vbOKOnly
cn.Close Set cn = Nothing
End Su
|