2012-02-24 244 views
1

我試圖在訪問中導入逗號分隔的csv文件。我遇到的問題是其中一列「金額」在數據本身中有逗號,例如「1,433.36」。這些數據中總會有逗號。用逗號分隔多個逗號分隔的csv文件

我怎樣才能導入成功?

樣本數據:

sjonn,one,"1,855.9" 
ptele,two,344.0 
jrudd,one,334.8 

在此先感謝

+0

@HansUp有模塊,它是一個擴展之前,首先拆分字符串成線那個問題。我已使用示例數據更新了上述問題 – user793468 2012-02-24 21:41:19

+0

請確認:如果金額字段包含逗號,則該值用雙引號括起來。否則,金額值**不會被引號包圍。 – HansUp 2012-02-24 21:52:40

+0

@HansUp是的,正確的 – user793468 2012-02-24 22:01:29

回答

1

我會分隔符更改爲一個不同的角色,就像一個管道 「|」。

+0

我無法控制我收到的文件 – user793468 2012-02-24 19:39:54

0

將該文件另存爲製表符分隔的文本文件並導入。

+0

我無法控制我收到的文件 – user793468 2012-02-24 19:40:28

+0

您可以在Excel中打開它並將其保存爲其他格式 – Paul 2012-02-24 19:56:47

+0

我正在設計一個用戶界面,使用戶可以選擇我正在說的文件,然後在Access Table中顯示它,是的,我可以打開它並以不同的格式重新保存它,但是我將遇到所有其他複雜問題,例如:將用戶的計算機保存在哪裏,用戶是否有一個特定的文件夾,用戶是否有權訪問該特定的文件夾等。因此,尋找一個不同的解決方案 – user793468 2012-02-24 20:19:46

0

使用輸入讀取文件處理的報價爲您

Dim f1 As String 
Dim f2 As String 
Dim f3 As String 

Open "d:\test.txt" For Input As #1 

Input #1, f1, f2, f3 
Debug.Print f1, f2, f3 
Input #1, f1, f2, f3 
Debug.Print f1, f2, f3 

Close #1 ' 

sjonn   one   1,855.9 
ptele   two   344.0 
1

如果DoCmd.TransferText不爲你工作,那麼你可以定義一個方法,這樣做「手動」 :

Set fs = Server.CreateObject("Scripting.FileSystemObject") 
Set objFile = fs.GetFile("import.txt") 
Set objFileTextStream = objFile.OpenAsTextStream(1, 2) 

objFileTextStream.skipLine 'if the file contains the header 

Do While objFileTextStream.AtEndOfStream <> True 
    strLine = objFileTextStream.ReadLine 'read a line 
    strLinePart = split(strLine,",") 'Split the line using the , delimiter 
    firstField = strLinePart(0) 
    secondField = strLinePart(1) 
    thirdField = strLinePart(2) 
    strSQL = "INSERT INTO myTable Values('"& firstField &"','"& secondField &"','"& thirdField &"')" 
    conn.Execute strSQL 
Loop 

objFileTextStream.Close: Set objFileTextStream = Nothing 
Set fs = Nothing 
conn.Close: Set conn = Nothing 
0

我曾經遇到過這個問題,這是另一種方法,也許會有幫助,但是它的分割線本身,即你必須使用這種方法 其還假定它包含在一個命名爲模塊

''Perfoms a smart split that takes care of the "" 
    Public Function SmartSplit(Str As String) As Variant 

    ''New collection 
    Dim Quote As String 
    Dim Delimiter As String 
    Dim MyString As String 
    Dim Sample As String 
    Dim StrCollection As New Collection 
    Dim Array_1() As String 
    Dim HasSeenQuote As Boolean 
    Dim index As Long 

    Quote = "" & CStr(Chr(34)) 
    Delimiter = "" & CStr(Chr(44)) 
    HasSeenQuote = False 



    Array_1 = Split(Str, Delimiter) 


    For index = LBound(Array_1) To UBound(Array_1) 

    Sample = Array_1(index) 

    If Module1.StartsWith(Sample, Quote, False) Then 
    HasSeenQuote = True 
    End If 

    ''We append the string 
    If HasSeenQuote Then 
    MyString = MyString & "," & Sample 
    End If 


    ''We add the term 
    If Module1.EndsWith(Sample, Quote, False) Then 
    HasSeenQuote = False 

     MyString = Replace(MyString, Quote, "") 
     MyString = Module1.TrimStartEndCharacters(MyString, ",", True) 
     MyString = Module1.TrimStartEndCharacters(MyString, Quote, True) 
     StrCollection.Add (MyString) 
     MyString = "" 
     GoTo LoopNext 

    End If 

    ''We did not see a quote before 
    If HasSeenQuote = False Then 
      Sample = Module1.TrimStartEndCharacters(Sample, ",", True) 
      Sample = Module1.TrimStartEndCharacters(Sample, Quote, True) 
      StrCollection.Add (Sample) 
    End If 


    LoopNext: 
    Next index 



    ''Copy the contents of the collection 
    Dim MyCount As Integer 
    MyCount = StrCollection.Count 

    Dim RetArr() As String 
    ReDim RetArr(0 To MyCount - 1) As String 

    Dim X As Integer 
    For X = 0 To StrCollection.Count - 1 ''VB Collections start with 1 always 
     RetArr(X) = StrCollection(X + 1) 
    Next X 

    SmartSplit = RetArr 

    End Function 


    ''Returns true of false if the string starts with a string 
    Public Function EndsWith(ByVal Str As String, Search As String, IgnoreCase   As   Boolean) As Boolean 

    EndsWith = False 
    Dim X As Integer 
    X = Len(Search) 

    If IgnoreCase Then 
    Str = UCase(Str) 
    Search = UCase(Search) 
    End If 


    If Len(Search) <= Len(Str) Then 

    EndsWith = StrComp(Right(Str, X), Search, vbBinaryCompare) = 0 

    End If 


    End Function 



    ''Trims start and end characters 
    Public Function TrimStartEndCharacters(ByVal Str As String, ByVal Search As   String, ByVal IgnoreCase As Boolean) As String 

    If Module1.StartsWith(Str, Search, IgnoreCase) Then 
    Str = Right(Str, (Len(Str) - Len(Search))) 
    End If 

    If Module1.EndsWith(Str, Search, IgnoreCase) Then 
     Str = Left(Str, (Len(Str) - Len(Search))) 
    End If 

    TrimStartEndCharacters = Str 

    End Function 


    ''Returns true of false if the string starts with a string 
    Public Function StartsWith(ByVal Str As String, Search As String, IgnoreCase As Boolean) As Boolean 

    StartsWith = False 
    Dim X As Integer 
    X = Len(Search) 

    If IgnoreCase Then 
    Str = UCase(Str) 
    Search = UCase(Search) 
    End If 


    If Len(Search) <= Len(Str) Then 

    StartsWith = StrComp(Left(Str, X), Search, vbBinaryCompare) = 0 

    End If 


    End Function