By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Saving a dataset as a comma seperated values file.

P: n/a
Hi,

This must be simple (I hope)! I've tried a google search but can't seem to
find what I want (probably using the wrong search keys!)

I'm trying to simply save a complete dataset as a CSV file so that it can be
easily opened up in Excel or emailed on.

Is there any simple way of doing this, or do I have to write code to scan
through the dataset and write it out line by line to a csv file?

Thanks in advance

Simon
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Simon,

Scorpion(Kelly) has on his side a page to set it directly to an Excel file.

http://www.kjmsolutions.com/datasetarray.htm

I have this samplecode bellow what is queit generic to set a dataset
datatable to a csv file.

I hope this helps a little bit?

Cor

\\\
Dim sw As New IO.StreamWriter("C:\Whatever.csv")
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim row As New System.Text.StringBuilder
row.Append("""")
For y As Integer = 0 To ds.Tables(0).Columns.Count - 1
row.Append(ds.Tables(0).Rows(i)(y).tostring)
If y <> ds.Tables(0).Columns.Count - 1 Then
row.Append(""",""")
' if you want it with a tab
' row.Append("""")
' row.Append(chr(09))
' row.Append("""")
Else
row.Append("""")
End If
Next
sw.WriteLine(row.ToString)
Next
sw.Flush()
sw.Close()
///
Nov 21 '05 #2

P: n/a
Thanks Cor,

That is pretty much what I thought I would have to do ...

regards
Simon
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:OW*************@TK2MSFTNGP12.phx.gbl...
Simon,

Scorpion(Kelly) has on his side a page to set it directly to an Excel file.
http://www.kjmsolutions.com/datasetarray.htm

I have this samplecode bellow what is queit generic to set a dataset
datatable to a csv file.

I hope this helps a little bit?

Cor

\\\
Dim sw As New IO.StreamWriter("C:\Whatever.csv")
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim row As New System.Text.StringBuilder
row.Append("""")
For y As Integer = 0 To ds.Tables(0).Columns.Count - 1
row.Append(ds.Tables(0).Rows(i)(y).tostring)
If y <> ds.Tables(0).Columns.Count - 1 Then
row.Append(""",""")
' if you want it with a tab
' row.Append("""")
' row.Append(chr(09))
' row.Append("""")
Else
row.Append("""")
End If
Next
sw.WriteLine(row.ToString)
Next
sw.Flush()
sw.Close()
///

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.