【问题标题】:ADODB Connection String for .csv.csv 的 ADODB 连接字符串
【发布时间】:2011-11-11 01:43:09
【问题描述】:

我想在 Excel VBA 中使用 ADODB 处理 .csv 文件。我尝试了一些在网上找到的字符串,但它们似乎都不起作用。我正在使用以下方式获取文件路径:

strVFile = Application.GetOpenFilename("CSV (*.csv), *.csv")

然后我将strVFile 作为参数传递给子objReport.Load strVFile。子标题为:Public Sub Load(ByVal strFilename As String)

然后我尝试使用字符串进行 ADODB 连接:

pconConnection.ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilename & _
            ";Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False"
    pconConnection.Open

当我运行宏并选择 CSV 文件时,出现“给定路径不是有效路径”的错误消息。我做错了什么?

编辑(代码)

模块 mdlReport

Public Sub Report()
    Dim objReport As clsReport


    MsgBox "Please select .csv file", vbInformation + vbOKOnly
    strVFile = Application.GetOpenFilename("CSV (*.csv), *.csv")

    If strVFile <> False Then
        Set objReport = New clsReport

        objReport.Load strVFile

    End If
End Sub

类 clsReport

Private pconConnection As ADODB.Connection
Private prstRecordset As ADODB.Recordset

Private Sub Class_Initialize()
  Set pconConnection = New ADODB.Connection
  pconConnection.ConnectionTimeout = 40
End Sub

Public Sub Load(ByVal strFilename As String)

    pconConnection.ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilename & _
            ";Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False"
    pconConnection.Open

End Sub

【问题讨论】:

  • 您能否发布您的完整代码以便我们对其进行测试?
  • 暂时使用Debug.PrintMsgBox 来显示strFilename 的值,然后再在连接字符串中使用它。检查它是否显示所需文件的完整路径。另外,Delimited(;) 应该是 Delimited(,) 对于我认为的 CSV
  • 我添加了一些代码,只有几行,但它应该显示我的问题。我试着看了一下路径,似乎没问题。

标签: excel vba connection-string adodb


【解决方案1】:

对于文本文件,Data Source 是文件夹,而不是文件。该文件是表(SELECT * FROM ..)。见http://www.connectionstrings.com/textfile

【讨论】:

    【解决方案2】:

    我找到了问题的答案。对于文本文件(如 Remou 所述)Data Source 只是文件夹路径,没有文件名。另外而不是使用:

    C:\dir\dir2\
    

    我不得不使用

    C:\\dir\\dir2\\
    

    从完整路径获取文件名:

    strFilename = Dir(strFilepath)
    

    只获取路径,不带文件名:

    strFilepath = Left$(strFilepath, InStrRev(strFilepath, "\"))
    

    将路径格式从 '\' 更改为 '\\' 我刚刚使用:

    strFilepath = Replace(strFilepath, "\", "\\")
    

    问题已解决,感谢关注。

    【讨论】:

      【解决方案3】:

      这是使用 Microsoft.ACE.OLEDB.16.0 作为提供程序的更新。

      确保首先将参考库“Microsoft ActiveX Data Objects 6.1 Library”添加到 VBAproject。

      Sub testrunSQLQueryForCSV()
          Dim arrayTest
          arrayTest = runSQLQueryForCSV("C:\xxx\yyyy\", "SELECT * FROM mycsvfile.csv")
      
          'NOTE: for CSV the Data Source reference is to the directory and the csv file is similar
          'to one "worksheet" in an excel file and is simply referenced in the SQL statement
      End Sub
      
      
      Public Function runSQLQueryForCSV(fileDirPath As String, SQLStatement As String)
      
          Dim Conn As New ADODB.Connection
          Dim RecSet As New ADODB.Recordset
      
          With Conn
             .Provider = "Microsoft.ACE.OLEDB.16.0"  'Can use many providers, but this is the latest and it works with csv files also
             .ConnectionString = "Data Source=" & fileDirPath & ";Extended Properties='text'"
          End With
      
          Conn.Open
      
          RecSet.Open SQLStatement, Conn
          runSQLQueryForCSV = RecSet.GetRows()
      
          Conn.Close
          Set RecSet = Nothing
          Set Conn = Nothing
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 2019-07-27
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多