/// <summary>
/// 将DataTable中数据写入到CSV文件中
/// </summary>
/// <param name="dt">提供保存数据的DataTable</param>
/// <param name="fileName">CSV的文件路径</param>
public void SaveCSV(DataTable dt, string fileName)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".csv");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType = "application/excel";
StringBuilder sb = new StringBuilder();
sb.Append("序号,经销商ID,登录账号,登录名,使用终端,登录时间,操作系统,经销商名称,登录角色,经销商地区,浏览器,网络类型,登录时长
");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j<= dt.Columns.Count - 1; j++)
{
sb.Append(""" + dt.Rows[i][j] + """ + ",");
}
sb.Remove(sb.Length - 1, 1).Append("
"); ;//移出掉最后一个,字符
}
Response.Write(sb.ToString());
Response.End();
}