.NET 简单导出CSV文件

时间:2023-12-31 20:23:26
        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";
System.Text.StringBuilder sb = new System.Text.StringBuilder(); /* 这里*需要添加数据 */
sb.Append("ID,title,content\r\n"); //标题
sb.Append("1,标题,内容\r\n"); //行 /*
//用DataTable获取的数据
string str = "";
foreach (System.Data.DataRow dr in dt.Rows)
{
str = "";
foreach (object r in dr.ItemArray)
{
str += r.ToString() + ",";
}
str = str.Substring(0, str.Length - 1) + "\r\n";
sb.Append(str);
}
*/ Response.Write(sb.ToString());
Response.End();