|
代码如下:- Sub Ado0()
- Dim cnn As New ADODB.Connection
- Dim rst As New ADODB.Recordset
- Dim Sql As String
- Dim lujing As String
- Dim i As Integer
- Dim j As Integer
- lujing = ThisWorkbook.Path & Application.PathSeparator & "通讯录.xls"
- cnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties='Excel 8.0';Data Source=" & lujing
- Sql = "Select 姓名 From [Sheet1$] "
- rst.Open Sql, cnn, adOpenKeyset
- For i = 1 To rst.Fields.Count
- Sheet1.Cells(1, i) = rst.Fields(i - 1).Name
- Next i
- rst.Close
- cnn.Close
- Set rst = Nothing
- Set cnn = Nothing
- End Sub
复制代码 运行后如此这般:
恳请高手帮助!!
本帖最后由 hwc2ycy 于 2013-1-28 23:57 编辑
- Sub Ado0()
- Dim cnn As New ADODB.Connection
- Dim rst As New ADODB.Recordset
- Dim Sql As String
- Dim lujing As String
- Dim i As Integer
- Dim j As Integer
- ActiveSheet.UsedRange.Clear
-
- lujing = ThisWorkbook.Path & Application.PathSeparator & "通讯录.xls"
- cnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"";Data Source=" & lujing
- Sql = "Select * From [Sheet1$] "
- rst.Open Sql, cnn, adOpenKeyset
- For i = 1 To rst.Fields.Count
- Sheet1.Cells(1, i) = rst.Fields(i - 1).Name
- Next i
- Range("a2").CopyFromRecordset rst
- rst.Close
- cnn.Close
- Set rst = Nothing
- Set cnn = Nothing
- MsgBox "查询完毕"
- End Sub
复制代码
|
|