我需要将值从datatable导出到csv格式。我的代码工作正常,但是当列值很大时,它会转到下一行。完全对齐正在变化.Below是我的代码。请帮助。< / p>
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
// Create the CSV file to which grid data will be exported.
string filename = "Report";
string test_Filepath = "C:\\Desktop\\";
string file1 = filename + "_" + String.Format("{0:dd}", DateTime.Today) + String.Format("{0:MMMM}", DateTime.Today) + String.Format("{0:yyyy}", DateTime.Today) + ".csv";
string new1 = test_Filepath + file1;
FileInfo file = new FileInfo(new1);
if (file.Exists == true)
file.Delete();
StreamWriter sw = new StreamWriter(new1, false);
//First we will write the headers.
int iColCount = dtMainSQLData.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dtMainSQLData.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dtMainSQLData.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Environment.Exit(0);
}