Solved

Creating a tab delimited text file in VB.NET

Posted on 2011-03-02
5
2,737 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

772 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