我要导入Excel数据到SQL Server,我是把Excel作为数据源。查询出的数据是从Excel中的第二行数据开始的,第一行数据作为表头了。如果我所选择的Excel文件没有表头(即第一行就是数据),那么我如何把表头(第一行数据)也查询出来?那位高手帮忙解决?先谢谢了!

解决方案 »

  1.   

    这是Els to mdb 的,你只修改 mCnnStringA 为SQL 连接就可以了
    -----------------------------------------------------------
    Option Explicit
        Public mCnnStringE As String
        Public mCnnStringA As StringPrivate Sub Form_Load()
        mCnnStringE = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= " & App.Path & "\Book1.xls;" & "Extended Properties=""Excel 8.0;HDR=Yes;"";"
        mCnnStringA = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb" & ";Persist Security Info=False"
    End SubPrivate Sub Command1_Click()
        Dim mConE As New ADODB.Connection
        Dim mRstE As New ADODB.Recordset
        Dim mConA As New ADODB.Connection
        Dim mRstA As New ADODB.Recordset
        Dim i As Integer
        mConE.Open mCnnStringE
        mConA.Open mCnnStringA
        mRstE.CursorLocation = adUseClient
        mRstA.CursorLocation = adUseClient
        mRstE.Open "Select * From [Sheet1$] Where mDate Between #" & "2003-12-11#" & "And #" & "2010-12-11#", mCnnStringE, adOpenStatic, adLockOptimistic
        mRstA.Open "Select * From mTable Where 1 = 0", mCnnStringA, adOpenStatic, adLockOptimistic
        Do Until mRstE.EOF
            mRstA.AddNew
            For i = 0 To mRstE.Fields.Count - 1
                mRstA.Fields.Item(i).Value = mRstE.Fields.Item(i).Value
            Next i
            mRstE.MoveNext
        Loop
        mRstA.Update
        Set mRstE = Nothing
        Set mConE = Nothing
        Set mRstA = Nothing
        Set mConA = Nothing
    End Sub