.NET 简单导出CSV文件

        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(); 

 

posted @ 2018-04-17 15:31  黑色老猫。  阅读(565)  评论(0编辑  收藏  举报