【问题标题】:How to import a CSV file when its name contains dots?当名称包含点时如何导入 CSV 文件?
【发布时间】:2021-05-06 12:26:03
【问题描述】:

我需要通过脚本将 .csv 文件从只读目录导入到 Access 表中。

名称中带有点的文件失败:例如file.le.name.csv.

我找到了这些解决方案:

  1. 重命名文件
  2. 复制文件并为其命名,不带点

是否有可能以不同的方式解决它?

Dim strSelect as String
Dim strFile as String 
Dim strConnectionString as String
Dim strFolder as String
Dim rs as ADODB.Recordset
Dim cn as ADODB.Connection
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection

strFolder = "C:\path"

strConnectionString = "Provider=" & _
  CurrentProject.Connection.Provider & _
  ";Data Source=" & strFolder & Chr(92) & _
  ";Extended Properties='text;HDR=YES;FMT=Delimited'"

cn.Open strConnectionString

strFile = "fi.le.name.csv"
strSelect = "SELECT * FROM " & strFile

rs.Open strSelect, cn, adOpenForwardonly 'and here is the failure

【问题讨论】:

  • 需要声明rs和cn变量——你设置了rn但是打开了rs。可以设置 CSV 文件的链接并使用链接表将数据拉入本地表。
  • 你在模块头中有 Option Explicit 吗?它将帮助您发现变量名的拼写错误。
  • 是的,我有。抱歉,我正在从另一个未连接到互联网的系统中重新输入部分代码。

标签: vba csv ms-access


【解决方案1】:

将文件复制到另一个文件夹并通过删除点重命名。连接语法也在另一个文件夹中指定。

Sub test()
    Dim strSelect As String
    Dim strFile As String
    Dim strConnectionString As String
    Dim strFolder As String
    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim strTarget As String
    
    Set rs = New ADODB.Recordset 
    Set cn = New ADODB.Connection
    
    strFolder = "C:\path"
    strTarget = "C:\"
    
    strConnectionString = "Provider=" & _
    "Microsoft.ACE.OLEDB.12.0" & _
    ";Data Source=" & strTarget & Chr(92) & _
    ";Extended Properties='text;HDR=YES;FMT=Delimited'"
    
    cn.Open strConnectionString
    
    strFile = "fi.le.name.csv" 
    Dim vF As Variant, newFile As String
    
    vF = Split(strFile, ".")
    newFile = Replace(strFile, vF(UBound(vF)), "")
    newFile = Replace(newFile, ".", "") & "." & vF(UBound(vF))
    
    FileCopy strFolder & "\" & strFile, strTarget & newFile
    
    strSelect = "SELECT * FROM " & newFile
    
    rs.Open strSelect, cn 'and here is the failure
    
    Debug.Print rs(0)
  
End Sub

【讨论】:

  • 我认为不涉及 Excel,因此 CopyFromRecordset 不相关。使用Debug.Print rs(0) 进行测试。测试了您的版本并得到相同的错误。
  • @june7,好吧,我误会了。
【解决方案2】:

我尝试使用短名称,它对我有用:

    Sub dots()
    Dim strSelect As String
    Dim strFile As String
    Dim strConnectionString As String
    Dim strFolder As String
    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Set rs = New ADODB.Recordset
    Set cn = New ADODB.Connection
    
    strFolder = "c:\Users\Alex20\Documents" ' my path
    
    strConnectionString = "Provider=" & _
    "Microsoft.ACE.OLEDB.12.0" & _
    ";Data Source=" & strFolder & _
    "\;Extended Properties='text;HDR=YES;FMT=Delimited'"
    
    'Debug.Print strConnectionString
    
    cn.Open strConnectionString
    
    strFile = "fi.le.name.csv"
    strFile = ShortName(strFile)
    strSelect = "SELECT * FROM `" & strFile & "`"
    
    Debug.Print strSelect   'SELECT * FROM `FILENA~1.CSV`
    rs.Open strSelect, cn, adOpenForwardonly 'and here is the failure
    
    Do While Not rs.EOF
        Debug.Print rs.Fields(0), rs.Fields(1)
        rs.MoveNext
    Loop
End Sub

Function ShortName(filespec)
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(filespec)
    ShortName = f.ShortName
End Function

【讨论】:

  • 谢谢你,阿列克谢。我会使用你的解决方案。
  • 'ShortName'函数的文件路径'filespec'必须是全路径,不能只写文件名,否则会报错。
猜你喜欢
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
相关资源
最近更新 更多