sqlserver导出为EXcel--CSV格式

 DataTable dt = Connect.BindTable(" SELECT  名称,地址,当前日期  FROM  table   GROUP BY 名称,地址,当前日期 order by 名称,地址,当前日期 desc  ");
  Export(dt);

 /*exel文件导入函数*/
    private void Export(System.Data.DataTable tab)
    {
        StringWriter sw = new StringWriter();
        sw.WriteLine("名称,地址,当前日期");
        foreach (DataRow dr in tab.Rows)
        {
            sw.WriteLine(dr["名称"] + "," + dr["地址"] + "," + dr["当前日期"] );
        }
        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
    }

原文地址:https://www.cnblogs.com/accumulater/p/6698490.html