我在将数据转换为 csv 时遇到问题,尤其是格式日期“dd-MM-yyyy”。
public void Export_CSV(DataTable dt, string FileName, string Separator, string Path)
{
string path = HttpContext.Current.Server.MapPath("~/" + Path + "/" + FileName + ".csv");
StringBuilder sb = new StringBuilder();
string[] columnNames = dt.Columns.Cast<DataColumn>().
Select(column => column.ColumnName).
ToArray();
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
string[] fields = row.ItemArray.Select(field => field.ToString()).
ToArray();
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(path, sb.ToString());
}
结果,格式日期不是恒定的。它应该像“dd-MM-yyyy”
阿晨1998
相关分类