Write selected columns into CSV

9 views
Skip to first unread message

heartland

unread,
Sep 4, 2008, 2:27:13 PM9/4/08
to CSVChat
Is there a easy way to write SELECTED columns of a table or DataSet
into a CSV file?
Thanks.

heartland

shriop

unread,
Sep 4, 2008, 2:32:00 PM9/4/08
to CSVChat
It depends on what you call easy. It really should just be a matter of
modifying the code from the WriteAll method below to do what you want.

Bruce Dunwiddie

public void WriteAll(DataTable data, bool writeHeaders)
{
if (data != null)
{
if (writeHeaders)
{
foreach (DataColumn column in data.Columns)
{
Write(column.ColumnName);
}

EndRecord();
}

int columnCount = data.Columns.Count;
int rowCount = data.Rows.Count;

foreach (DataRow row in data.Rows)
{
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
Write(row[columnIndex].ToString());
}

EndRecord();
}

outputStream.Flush();
}
}

to something like

public void WriteAll(DataTable data, bool writeHeaders)
{
if (data != null)
{
if (writeHeaders)
{
foreach (DataColumn column in data.Columns)
{
if (column.ColumnName.StartsWith("Raw"))
{
Write(column.ColumnName);
}
}

EndRecord();
}

int columnCount = data.Columns.Count;
int rowCount = data.Rows.Count;

foreach (DataRow row in data.Rows)
{
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
if (data.Columns[columnIndex].ColumnName.StartsWith("Raw"))
{
Write(row[columnIndex].ToString());
}
}

EndRecord();
}

outputStream.Flush();

Lu, Hugh

unread,
Sep 4, 2008, 2:53:40 PM9/4/08
to CSV...@googlegroups.com
Bruce,

Thanks very much for quick reply. Your way is indeed easy enough. I was thinking of even easier way of use no loop, something like "WriteAll" with a column name as qualifier.

Hugh
Lab Automation Systems Group
Research Information Management
x4232
Building # 36
This communication is for use by the intended recipient and contains
information that may be Privileged, confidential or copyrighted under
applicable law. If you are not the intended recipient, you are hereby
formally notified that any use, copying or distribution of this e-mail,
in whole or in part, is strictly prohibited. Please notify the sender by
return e-mail and delete this e-mail from your system. Unless explicitly
and conspicuously designated as "E-Contract Intended", this e-mail does
not constitute a contract offer, a contract amendment, or an acceptance
of a contract offer. This e-mail does not constitute a consent to the
use of sender's contact information for direct marketing purposes or for
transfers of data to third parties.

Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean

http://www.DuPont.com/corp/email_disclaimer.html

Reply all
Reply to author
Forward
0 new messages