您好我正在尝试使用adodb从csv文件中获取一些数据。尝试将记录复制到当前工作表我能够从源文件中复制标题而不是下面的记录
非常感谢一些帮助。
Public Sub ConnectExcel()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Dim recCount As Long
Dim fc As Long
Dim ic As Integer
Dim ir As Integer
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
'***********CONNECTION TO THE FILE********************'
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=C:\tmp\;Extended Properties='text;HDR=YES;FMT=Delimited';"
.Open .ConnectionString
End With
'***********READING RECORDS********************'
str = "SELECT TOP 10 * FROM [prod.csv] WHERE [Netting Agreement Type]='GROSS'"
rs.Open str, cn
rs.MoveFirst
While Not rs.EOF
recCount = recCount + 1
rs.MoveNext
Wend
'***********COPY RECORDS********************'
fc = rs.Fields.Count
For ic = 1 To fc
ThisWorkbook.Worksheets("Sheet1").Cells(1, ic).Value = rs.Fields(ic - 1).Name
Next
ThisWorkbook.Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rs
'ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).CopyFromRecordset rs
'Print rs.RecordCount
'***********CLOSE CONNECTIONS AND RELESE REFERENCES********************'
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
答案 0 :(得分:0)
想出来。抱歉,可能还有其他代码可以继续使用,但希望有一天能帮助其他人。
Public Sub ConnectExcel()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Dim str2 As String
Dim str3 As String
Dim imm As Variant
Dim cem As Variant
Dim ic As Integer
'***********CONNECTION TO THE FILE********************'
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=C:\tmp\;Extended Properties='text;HDR=YES;FMT=Delimited';"
.Open .ConnectionString
End With
'***********READING RECORDS FROM CSV AND STORING THEM TO VARIANTS********************'
'str = "SELECT TOP 10 I.*, C.* FROM [hkmaimm.csv] I INNER JOIN [hkmacem.csv] C ON I.[Counterparty ID] = C.[Counterparty ID] AND I.[Saracen] = C.[Saracen]"
'***********READ AND STORE IMM CONTEXT********************'
'str1 = "SELECT TOP 500 * FROM [hkmaimm.csv]"
str1 = "SELECT * FROM [hkmaimm.csv]"
rs.Open str1, cn
imm = rs.GetRows()
fc = rs.Fields.Count
For ic = 1 To fc
ThisWorkbook.Worksheets("Sheet2").Cells(1, ic).value = rs.Fields(ic - 1).Name
Next
fc = 0
rs.Close
'***********READ AND STORE CEM CONTEXT********************'
'str2 = "SELECT TOP 500 * FROM [hkmacem.csv]"
str2 = "SELECT * FROM [hkmacem.csv]"
rs.Open str2, cn
cem = rs.GetRows()
fc = rs.Fields.Count
For ic = 1 To fc
ThisWorkbook.Worksheets("Sheet3").Cells(1, ic).value = rs.Fields(ic - 1).Name
Next
rs.Close
Call CalculateLGDchange(imm, cem)
'***********COPY RECORDS********************'
'
'rs.MoveFirst
'RowCount = 2
'While Not rs.EOF
' For col = 1 To rs.Fields.Count
' ThisWorkbook.Worksheets("Sheet1").Cells(RowCount, col) = rs(col - 1)
' Next col
' RowCount = RowCount + 1
' rs.MoveNext
'Wend
'
'***********CLOSE CONNECTIONS AND RELEASE REFERENCES********************'
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub