Solved

Creating a tab delimited text file in VB.NET

Posted on 2011-03-02
5
2,670 Views
Last Modified: 2012-05-11
I have a datset with a single data table, and I would like to write the rows of data to a tab delimited text file.
Any source code would be great.
0
Comment
Question by:countrymeister
5 Comments
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 100 total points
ID: 35024040
DataTable2CSV - You can Save DataTable to Text File. you can change Seperator character as per your requirement.

Sample Code
http://www.devx.com/vb2themax/Tip/19703
0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 50 total points
ID: 35024063
Hi,

Here is an example, you just have to add the

Imports System.IO

To your class
Dim dtTable As DataTable
        'Getting the Table from the Dataset
        dtTable = DataSet1.Tables(0)
        'Object that holds the File 
        Dim swrTXT As New StreamWriter("C:\TabDelimitedFile.txt")
        Dim drRow As DataRow
        Dim intCol As Int32
        Dim strLine As String
        'Delimiter Char
        Dim chrTAB As Char = Chr(Keys.Tab)

        'Loop all the rows in the table
        For Each drRow In dtTable.Rows
            strLine = ""
            'Loop all the columns to create the Line
            For intCol = 0 To dtTable.Columns.Count - 1
                'If is the last column do not add the TAB Delimiter
                If intCol = dtTable.Columns.Count - 1 Then
                    strLine &= drRow(intCol).ToString
                Else 'if not is the last column then ADD the TAB Delimiter
                    strLine &= drRow(intCol).ToString & chrTAB
                End If
            Next
            'Adding the line to the file
            swrTXT.WriteLine(strLine)
        Next

        'Saving the changes to the file
        swrTXT.Flush()
        swrTXT.Close()
        swrTXT.Dispose()

Open in new window

0
 
LVL 8

Assisted Solution

by:PagodNaUtak
PagodNaUtak earned 50 total points
ID: 35024079
Here is a sample:
If myReader.HasRows Then
  Do While myReader.Read()
    Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
  Loop
Else
  Console.WriteLine("No rows returned.")
End If

myReader.Close()
[C#]
if (myReader.HasRows)
  while (myReader.Read())
    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
else
  Console.WriteLine("No rows returned.");

myReader.Close();

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 50 total points
ID: 35024121
Easy enough to just loop through the rows of the table and write the value of each column, plus a tab, to a text file.  I didn't bother to read that link above, so this may very well be the same thing. ;)

Imports System.Data
Imports System.IO

Module Module1

	Sub Main()
		'This sets up a dummy dataset with some
		'dummy data just for testing
		Dim table As New DataTable("testTable")
		table.Columns.Add("ID", Type.GetType("System.Int32"))
		table.Columns.Add("Kind", Type.GetType("System.String"))
		table.Columns.Add("Name", Type.GetType("System.String"))
		table.Rows.Add(1, "Animal", "Cat")
		table.Rows.Add(2, "Animal", "Dog")
		table.Rows.Add(3, "Vehicle", "Car")
		table.Rows.Add(4, "Vehicle", "Airplane")
		table.Rows.Add(5, "Vehicle", "Boat")
		table.Rows.Add(6, "Crayon", "Purple")
		table.Rows.Add(7, "Crayon", "Edible")

		Dim ds As New DataSet()
		ds.Tables.Add(table)

		ExportTable(ds.Tables("testTable"), "C:\test.txt")
	End Sub

	Sub ExportTable(ByVal table As DataTable, ByVal exportFile As String)
		Using exportStream As New StreamWriter(exportFile)
			Dim columncount = table.Columns.Count
			For Each r As DataRow In table.Rows
				For i As Integer = 0 To columncount - 2
					exportStream.Write("{0}{1}", r(i), vbTab)
				Next
				exportStream.WriteLine("{0}", r(columncount - 1))
			Next
		End Using
	End Sub
End Module

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35024127
Whoa...I guess I must type slow!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now