2012-02-21 11 views
0

.csvデータソースから混合データ型を読み取るときに問題があります。String/Numeric値が混在する列がある場合、文字列はNULLとして返されます。 IMEX = 1に設定し、レジストリエントリのTypeGuessRowsを8から0に変更しました(ただし、最初の8行にデータ型が混在していても、文字列はNullとして渡されます)。 ImportMixedTypes =レジストリ内のテキスト。VBA/ADO:.csvデータソースから混合データ型を読み取る

私は何が欠けていますか?どんなアイデアも高く評価されました

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" _ 
    & "Data Source=" & Folder & ";" _ 
    & "Extended Properties='text;HDR=YES;FMT=CSVDelimited;IMEX=1';" _ 
    & "Persist Security Info=False;" 
+0

は、あなたがこれを読んだおかげでハイhttp://blog.lab49.com/archives/196 – assylias

答えて

0

あなたはADOでCSVを読み込むにロックされています:

は、ここに私の接続文字列ですか?私はいつもあなたが経験しているようにADOでテキストファイルを読み込もうとすると問題に遭遇するようです。私は通常、ADO側をあきらめて、より多くのコントロールを得るためにテキストリーダーで直接ファイルを読む。ここで

Public Sub TestIt() 

    Dim path As String 

    path = "C:\test.csv" 

    ReadText path 
End Sub 

Public Sub ReadText(path As String) 
'requires reference to 'Microsoft Scripting Runtime' scrrun.dll OR use late binding 

    Const DELIM As String = "," 
    Dim fso As New Scripting.FileSystemObject 
    Dim text As Scripting.TextStream 
    Dim line As String 
    Dim vals() As String 

    Set text = fso.OpenTextFile(path, ForReading) 

    Do While Not text.AtEndOfStream 

     line = text.ReadLine 

     vals = Split(line, DELIM) 

     'do something with the values 
    Loop 

    text.Close 
End Sub 
0

はFinkはもう少し柔軟性とエラー処理で、掲示ものと同様のADOを使用していない別のコードサンプル、です。パフォーマンスはそれほど悪くはありません(私のマシンで3秒以内に20 MBのcsvファイルを読み込んで解析します)。 (:ImportMixedTypes =テキスト下の郵便番号についての一部とresgistryキーを追加):

Public Function getDataFromFile(parFileName As String, parDelimiter As String, Optional parExcludeCharacter As String = "") As Variant 
'parFileName is supposed to be a delimited file (csv...)' 
'Returns an empty array if file is empty or can't be opened 
'number of columns based on the line with the largest number of columns, not on the first line' 
'parExcludeCharacter: sometimes csv files have quotes around strings: "XXX" - if parExcludeCharacter = """" then quotes are removed' 

    Dim locLinesList() As Variant 
    Dim locData As Variant 
    Dim i As Long 
    Dim j As Long 
    Dim locNumRows As Long 
    Dim locNumCols As Long 
    Dim fso As New FileSystemObject 
    Dim ts As TextStream 
    Const REDIM_STEP = 10000 

    On Error GoTo error_open_file 
    Set ts = fso.OpenTextFile(parFileName) 
    On Error GoTo unhandled_error 

    'Counts the number of lines and finds the largest number of columns' 
    ReDim locLinesList(1 To 1) As Variant 
    i = 0 
    Do While Not ts.AtEndOfStream 
    If i Mod REDIM_STEP = 0 Then 
     ReDim Preserve locLinesList(1 To UBound(locLinesList, 1) + REDIM_STEP) As Variant 
    End If 
    locLinesList(i + 1) = Split(ts.ReadLine, parDelimiter) 
    j = UBound(locLinesList(i + 1), 1) 'number of columns' 
    If locNumCols < j Then locNumCols = j 
    i = i + 1 
    Loop 

    ts.Close 

    locNumRows = i 

    If locNumRows = 0 Then Exit Function 'Empty file' 

    ReDim locData(1 To locNumRows, 1 To locNumCols + 1) As Variant 

    'Copies the file into an array' 
    If parExcludeCharacter <> "" Then 

    For i = 1 To locNumRows 
     For j = 0 To UBound(locLinesList(i), 1) 
     If Left(locLinesList(i)(j), 1) = parExcludeCharacter Then 
      If Right(locLinesList(i)(j), 1) = parExcludeCharacter Then 
      locLinesList(i)(j) = Mid(locLinesList(i)(j), 2, Len(locLinesList(i)(j)) - 2)  'If locTempArray = "", Mid returns ""' 
      Else 
      locLinesList(i)(j) = Right(locLinesList(i)(j), Len(locLinesList(i)(j)) - 1) 
      End If 
     ElseIf Right(locLinesList(i)(j), 1) = parExcludeCharacter Then 
      locLinesList(i)(j) = Left(locLinesList(i)(j), Len(locLinesList(i)(j)) - 1) 
     End If 
     Next j 
    Next i 

    Else 

    For i = 1 To locNumRows 
     For j = 0 To UBound(locLinesList(i), 1) 
     locData(i, j + 1) = locLinesList(i)(j) 
     Next j 
    Next i 

    End If 

    getDataFromFile = locData 

    Exit Function 

error_open_file: 'returns empty variant' 
unhandled_error: 'returns empty variant' 

End Function 
+0

が、ADOを使用する必要があり、必要にファイル上でSQLクエリを実行できるようにする –

+0

クエリによっては、返された配列を繰り返し処理していくつかの条件を確認するのは簡単かもしれませんが、その場合はADOを使用してやりたいと思います。 – assylias

関連する問題