2016-08-29 7 views
0

Accessにデータベースがあり、その情報をテキストファイルにエクスポートする必要があります問題は、正しい形式でエクスポートされていないことです。あなたが見ることができるように、私のコードではなく、キャリッジリターンの行の最後に縦線が挿入されてテキストファイルにエクスポートAccessから行末に改行がありません

info|info1|info3|info4|info5|info6|info7|info8| <this is the wrong format 
info|info1|info3|info4|info5|info6|info7|info8 <this is the correct format 

:これは、エクスポートされているテキストファイルの例です。

私のコードとすべてのヘルプは素晴らしいものだご覧ください。

Dim connetionString As String 
    Dim cnn As OleDbConnection 
    connetionString = "connection string.accdb;" 
    cnn = New OleDbConnection(connetionString) 

    Dim dtResult As New DataTable 
    cnn.Open() 
    'Change the query 
    Dim dataAdap As New OleDbDataAdapter("SELECT * FROM table", cnn) 
    dataAdap.Fill(dtResult) 
    cnn.Close() 

    'Change the path to your desired path 
    Dim RUTA As String = "path were i want to put the text file\" 
    Dim ARCHIVOTXT As String = "filename of the text file.txt" 

    If Not Directory.Exists(RUTA) Then 
     Directory.CreateDirectory(RUTA) 
    End If 

    Dim writer As New StreamWriter(RUTA + ARCHIVOTXT) 
    Try 
     Dim sb As New StringBuilder 
     For Each row As DataRow In dtResult.Rows 
      sb = New StringBuilder 
      For Each col As DataColumn In dtResult.Columns 
       sb.Append(row(col.ColumnName) & "|") 
      Next 
      writer.WriteLine(sb.ToString()) 
     Next 
    Catch ex As Exception 
     Throw ex 
    Finally 
     If Not writer Is Nothing Then writer.Close() 
    End Try 
    MsgBox("Done") 
End Sub 
+1

'Yourstring.Remove(Yourstring.Length - 1)'ちょっと速いです... – Codexer

答えて

1

の代わりに:

For Each col As DataColumn In dtResult.Columns 
     sb.Append(row(col.ColumnName) & "|") 
Next 
writer.WriteLine(sb.ToString()) 

あなただけ使用することができますwriter.WriteLine(String.Join("|", row.ItemArray))

String.Join("|", row.ItemArray)は、各値のToString()結果を連結値を「|」で区切る行に保持されます。

+0

最後の項目はどうですか、それもパイプはありませんか? – Codexer

+0

あなたがTnTinMnよりも、まさに私が探していたのはあなたの人生を保存してくれた:-) – pezzmedina

関連する問題