?
Solved

Creating a tab delimited text file in VB.NET

Posted on 2011-03-02
5
Medium Priority
?
3,219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 400 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 200 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 200 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 200 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 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