|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
如下图: 请大神指导该如何需改参数。谢谢!
Sub readCSV()
Dim conn As Object
Dim rs As Object
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
' 定义CSV文件路径
Dim csvFilePath As String
csvFilePath = ThisWorkbook.Path & "\客户信息.csv"
' 定义连接字符串,指定文本模式,并定义分隔符为逗号
Dim connectionString As String
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
ThisWorkbook.Path & ";Extended Properties='text;HDR=Yes;FMT=Delimited;CharacterSet=65001';"
' 打开连接
conn.Open connectionString
' 执行查询,从CSV文件中选择数据
rs.Open "SELECT * FROM [客户信息.csv]", conn, 1, 3 ' 1 = adOpenStatic, 3 = adCmdText
' 遍历记录集
While Not rs.EOF
For j = 0 To rs.fields.Count - 1
Debug.Print rs.fields(j).Name & ":" & rs.fields(j).Value
Next j
rs.MoveNext
Wend
' 关闭记录集和连接
rs.Close
conn.Close
' 清理对象
Set rs = Nothing
Set conn = Nothing
End Sub
|
|