Method:
private string ExportToCSVFile(DataTable dtTable)
{
StringBuilder sbldr = new StringBuilder();
if (dtTable.Columns.Count != 0)
{
foreach (DataColumn col in dtTable.Columns)
{
sbldr.Append(col.ColumnName + ',');
}
sbldr.Append("\r\n");
foreach (DataRow row in dtTable.Rows)
{
foreach (DataColumn column in dtTable.Columns)
{
sbldr.Append(row[column].ToString() + ',');
}
sbldr.Append("\r\n");
}
}
return sbldr.ToString();
}
Calling:
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() != DialogResult.OK)
{
this.Cursor = Cursors.Default;
return;
}
if (fbd.SelectedPath.Length <= 0)
{
this.Cursor = Cursors.Default;
return;
}
string path = System.IO.Path.Combine(fbd.SelectedPath, "Name_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv");
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
if (dt == null || dt.Rows.Count == 0)
{
return;
}
else
{
file.WriteLine( ExportToCSVFile(dt));
}
file.Close();
0 comments:
Post a Comment