2017-09-19 10 views
0

私は、CSVファイルをいくつか開き、そのファイルをDataTableとして、hereという例で読み込んでいます。私がデータをインポートするために使用している基本的なクエリを実行している問題は、IPアドレスの列をDoublesに変換することです。だから私は10.0.0.1で読みたいと思うし、10.001として表示されます。この列を文字列として読み込むにはどうすればよいですか?私はできればファイルを二重処理したくないです。私が使用しているCSVファイルをOLEDB接続として開くと、ファイルのテキストが二重に変換されます

クエリは基本的で、次のようになります。ここでは

SELECT * FROM [ComputerList.csv] 

は、私はCSV形式のを読むためにこれを使用したDataTable

Public Function OpenFile(ByVal strFolderPath as String, ByVal strQuery as String) as DataTable 
    Dim strConn as String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Propteries=""text; HDR=Yes;FMT=Delimited""" 
    Dim conn as OleDb.OleDbConnection = New OleDb.OleDbConnection(strConn) 

    Try 
     conn.Open() 
     Dim cmd as OleDb.OleDbCommand = New OleDb.OleDbCommand(strQuery, conn) 
     Dim da as OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter() 

     da.SelectCommand = cmd 
     Dim ds as DataSet = New DataSet() 
     da.Fill(ds) 
     da.Dispose() 

     return ds.Tables(0) 
    Catch 
     return Nothing 
    Finally 
     conn.Close() 
    End Try 

End Function 
+0

あなたのコードはどこですか?問題は明らかにこの行にはありません:) –

+0

[TextFieldParser](https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser?view= – peterG

+0

@MutedDisk私はコードを追加しました – Talguy

答えて

0

OKセパレーターと協力し、それらのすべての間のハイブリッドに落ち着いてきました。私の目標は、CSVファイルをDataTableフォームで操作して読み込み、それを書き戻すことでした。 CSVファイルの中には、セル内に複数の行があり、セル内にデリミネーターがあるものがあります。 TextFieldParserを利用してファイルを読み込んで分割するハイブリッドソリューションがあります。

Public Function OpenFile(ByVal File as String, NyVal delim as String) as DataTable 
    Dim dt as New DataTable() 
    Dim firstline as Boolean = True 
    Using MyReader as New Microsoft.VisualBasic.FileIO.TextFieldParser(File) 
     MyReader.TextFieldType = FileIO.FieldType.Delimited 
     MyReader.SetDelimiters(delim) 
     Dim currentRow as String() 

     While Not MyReader.EndOfData 
     Try 
      currentRow = MyReader.ReadFields() 

      If firstline 
       firstline = false 
       For Each col in currentRow 
        dt.Columns.Add(New DataColumn(col.ToString(), System.Type.GetType("System.String"))) 
       Next 
      Else 
       dt.Rows.Add(currentRow.ToArray()) 
      End If 
     Catch ex as Microsoft.VisualBasic.FileIO.MalformedLineException 
      Console.WriteLIne("Line " + ex.Message + " is not valid and will be skipped") 
     End Try 
     End While 
    End Using 

    return dt 
End Function 
1

にCSVファイルを開いて読むために私の関数でありますすべてを文字列に強制します。

Public Function convert_csv_to_data_table(ByVal File As String, ByVal separator As String) As DataTable 
    Dim dt As New DataTable 
    Dim firstLine As Boolean = True 
    If IO.File.Exists(File) Then 
     Using sr As New StreamReader(File) 
      While Not sr.EndOfStream 
       If firstLine Then 
        firstLine = False 
        Dim cols = sr.ReadLine.Split(separator) 
        For Each col In cols 
         dt.Columns.Add(New DataColumn(col, GetType(String))) 
        Next 
       Else 
        Dim data() As String = sr.ReadLine.Split(separator) 
        dt.Rows.Add(data.ToArray) 
       End If 
      End While 
     End Using 
    End If 
    Return dt 
End Function 

EDITは: - これが唯一の提案だ、私はみんなのバリエーションを試してみましたところで

+0

これを試してみましたが、他のほとんどのソリューションよりもうまく機能しましたが、セル内に複数の行が含まれ、デリミネーターも含まれていたCSVファイルではうまくいきました。私は、私の解決策でこれを少しずつやりました。 – Talguy

+0

それでも正しい方向にあなたを指摘してくれてうれしいです。 –

関連する問題