Re: DataSet to Text File by Jay
Jay
Thu Jan 22 08:06:19 CST 2004
scorpion53061,
I'm not sure if you are thanking me or asking for more help! (year ends bite
;-) )
The Export routine I gave should do the job for you. Simply change two lines
that look like:
> > delim = ","
to
> > delim = ControlChars.Tab
Then you can call the routine with:
> > Export("scorpion53061.txt", dslist1.Tables(0))
Well this isn't correct!
> > Dim output As New StreamWriter(path, False,
> UnicodeEncoding.Default)
> > You can change (or remove) the Encoding parameter above to suit your
> needs,
> > I used Unicode as the table had non ASCII characters in it.
I'm actually using the Encoding.Default property above, which gives me the
Win32 code page, which for this routine is more correct...
I would change the line to:
> > Dim output As New StreamWriter(path, False,
> Encoding.Default)
Hope this helps
Jay
"scorpion53061" <Its the end of the world as we know it@here.com> wrote in
message news:ePaU6fL4DHA.2348@TK2MSFTNGP10.phx.gbl...
> Jay,
>
> I hope you dont mind and I promise I will help you out someday but right
now
> my brain is mush and year end is hours away.
>
> My dataset name is dslist1.Tables(0)
>
> I have to write a tab delimted text file of this dataset.
>
> The dataset is large.
>
> Thank you for your help!!
>
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
> news:%23z$ArYL4DHA.1272@TK2MSFTNGP11.phx.gbl...
> > scorpion53061
> > Here's a quick VB.NET 1.1 export routine that is very general (too
> > general?):
> >
> > Its based on a DataSet, however you should be able to adopt it to a
> > DataReader instead.
> >
> > ' Required imports
> > Imports System.IO ' for the StreamWriter
> > Imports System.Text ' for the UnicodeEncoding
> >
> > ' sample usage
> > Export("Customers.csv", DataSet1.Tables("Customers"))
> > Export("Employees.csv", DataSet1.Tables("Employees"))
> >
> >
> > Public Sub Export(ByVal path As String, ByVal table As DataTable)
> > Dim output As New StreamWriter(path, False,
> UnicodeEncoding.Default)
> > Dim delim As String
> >
> > ' Write out the header row
> > delim = ""
> > For Each col As DataColumn In table.Columns
> > output.Write(delim)
> > output.Write(col.ColumnName)
> > delim = ","
> > Next
> > output.WriteLine()
> >
> > ' write out each data row
> > For Each row As DataRow In table.Rows
> > delim = ""
> > For Each value As Object In row.ItemArray
> > output.Write(delim)
> > If TypeOf value Is String Then
> > output.Write(""""c) ' thats four double quotes and a
c
> > output.Write(value)
> >
> > output.Write(""""c) ' thats four double quotes and a
c
> > Else
> > output.Write(value)
> > End If
> > delim = ","
> > Next
> > output.WriteLine()
> > Next
> >
> > output.Close()
> >
> > End Sub
> >
> > You can change (or remove) the Encoding parameter above to suit your
> needs,
> > I used Unicode as the table had non ASCII characters in it. Also when
> > writing strings, I don't deal with double quotes in the string. If you
> make
> > the StreamWriter a parameter it will be much more flexible (you could go
> to
> > a memory stream to support cut & paste). I use the default formatting
for
> > numeric types.
> >
> > You can change it to use a DataView (for sorting & filtering for
example)
> by
> > changing the following lines:
> >
> > 'Public Sub Export(ByVal path As String, ByVal table As DataTable)
> > Public Sub Export(ByVal path As String, ByVal view As DataView)
> >
> > 'For Each col As DataColumn In table.Columns
> > For Each col As DataColumn In view.Table.Columns
> >
> > 'For Each row As DataRow In table.Rows
> > For Each row As DataRowView In view
> >
> > 'For Each value As Object In row.ItemArray
> > For Each value As Object In row.Row.ItemArray
> >
> > Instead of setting the delim variable to "," for comma delimited, you
can
> > set it to ControlChars.Tab for tab delimited.
> >
> > Hope this helps
> > Jay
> >
> > "scorpion53061" <Its the end of the world as we know it@here.com> wrote
in
> > message news:OR4%23s9K4DHA.2480@TK2MSFTNGP09.phx.gbl...
> > > Well I had a way to write an array to an excel spreadsheet but on a
huge
> > > time critical run it failed iwth the dreaded HRESULT: 0x800A03EC
error.
> It
> > > worked fine when i sampled the data to go in but when it all tried to
go
> > in
> > > it bombed.
> > >
> > > So I need to write this to a tab delimited file and I sure hope
somebody
> > is
> > > awake tonight.
> > >
> > > How do I write my dataset to make a tab delimted text file?
> > >
> > >
> > >
> > >
> >
> >
>
>