VB 操作 EXCEL

'引用ADO(Microsoft ActiveX Data Objects 2.X Library)
Private Sub Command1_Click()
    On Error GoTo err
    Dim cn As New ADODB.Connection
    '具体的数据库连接请自己改过来
    cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=dataBase001;Data Source=mySERVICE"
    cn.CursorLocation = adUseClient
    cn.Open   
    cn.Execute "select * into NewTable from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=Yes;database=c:/Test.xls;','select * from [Sheet1$]')"
    cn.Close
    Set cn=Nothing
    Exit Sub
err:
    MsgBox err.Description
End Sub

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
Private Sub Command1_Click()
    Dim cn As New ADODB.Connection
    cn.CursorLocation = adUseClient
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "/test.xls;Extended Properties='Excel 8.0;HDR=Yes'"

    cn.Execute ("insert INTO [odbc;Driver={SQL Server};" & _
        "Server=ljx;Database=test;" & _
        "UID=sa;PWD=].sqlTable " & _
        "select * FROM [Sheet1$]")
    cn.Close
    Set cn = Nothing
End Sub
-----------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值