数据包含新行字符,因此每次将新行附加到导出的数据并且记录增加时,数据也包含“,”分隔值值 我实施的是这个解决方案
StringBuilder sb = new StringBuilder();
for (int k = 0; k < dt.Columns.Count; k++)
{
//add separator
sb.Append(dt.Columns[k].ColumnName + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
//string[] fields = dt.Rows[i].ItemArray.Select(field => field.ToString()).
// ToArray();
//sb.AppendLine(string.Join(",", fields));
//for (int k = 0; k < dt.Columns.Count; k++)
//{
var fields = dt.Rows[i].ItemArray.Select(field => field.ToString().WrapInQuotesIfContains(","));
sb.AppendLine(string.Join(",", fields));
//add separator
//sb.Append(dt.Rows[i][k].ToString() + ';');
//}
//append new line
//sb.Append("\r\n");`enter code here`
}