PC Review


Reply
Thread Tools Rate Thread

Data table export to CSV?

 
 
Smokey Grindel
Guest
Posts: n/a
 
      31st Jan 2008
Is it possible to export a single (not related to anything else) data
table's contents to a CSV file? I have XML export working fine, but they
want the files in CSV format.. thanks!


 
Reply With Quote
 
 
 
 
George Shubin
Guest
Posts: n/a
 
      31st Jan 2008
"Smokey Grindel" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Is it possible to export a single (not related to anything else) data
> table's contents to a CSV file?



Here's some code you can adapt to your needs: (watch out for word-wrap)



Public Sub DataTableToCSV(ByVal path As String, ByVal table As DataTable,
Optional ByVal ignoreColumn As Integer = -1)
'Export a single DataTable's contents to a CSV file
'Having the ability to ignore the outputting on one column (like a record
id)
Dim output As StreamWriter
Dim delim As String = ""

' Write out the header row
Try
output = New StreamWriter(path, False)
For Each col As DataColumn In table.Columns
If col.Ordinal <> ignoreColumn - 1 Then
output.Write(delim)
output.Write(col.ColumnName)
delim = ","
End If
Next
output.WriteLine()
Catch ex As Exception
MessageBox.Show("The data could not be output to the selected file name."
& vbCrLf & vbCrLf & vbCrLf & ex.Message, AppDesc, MessageBoxButtons.OK,
MessageBoxIcon.Information)
Exit Sub
End Try

' write out each data row
For Each row As DataRow In table.Rows
nTotRows = nTotRows + 1
Dim iCol As Integer = 0
delim = ""
For Each value As Object In row.ItemArray
iCol = iCol + 1
If iCol <> ignoreColumn Then
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 = ","
End If
Next
Try
output.WriteLine()
Catch ex As Exception
MessageBox.Show("The data could not be output to the selected file
name." & vbCrLf & vbCrLf & vbCrLf & ex.Message, AppDesc,
MessageBoxButtons.OK, MessageBoxIcon.Information)
frm.Close()
frm.Dispose()
Exit Sub
End Try
Next
Try
output.Close()
frm.Close()
frm.Dispose()
Catch ex As Exception
MessageBox.Show("The data could not be output to the selected file name."
& vbCrLf & vbCrLf & vbCrLf & ex.Message, AppDesc, MessageBoxButtons.OK,
MessageBoxIcon.Information)
Exit Sub
End Try

End Sub

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Pivot Table Data to Excel Table Milind Keer Microsoft Excel Misc 0 8th Oct 2008 04:53 PM
how to export data from a table (by clicking in table) to a form =?Utf-8?B?Um9oYWls?= Microsoft Access Forms 1 26th Sep 2007 07:54 PM
Export data to SQL table =?Utf-8?B?TWFyaw==?= Microsoft Excel Programming 1 6th Apr 2004 10:31 PM
Export table Data Sigh Microsoft Access External Data 2 19th Nov 2003 04:39 AM
Pivot table export data to table format Sue Moore Microsoft Excel Misc 1 2nd Sep 2003 11:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:27 AM.