zoukankan      html  css  js  c++  java
  • 导出到CSV文件乱码的问题

    http://social.microsoft.com/Forums/zh-CN/295/thread/14a833f5-95bf-48ef-b6cf-c6028f338561

    string name = System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString();
    FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));

    sw.WriteLine(" 自动编号,姓名,年龄");
    foreach (DataRow dr in dt.Rows)
    {
        sw.WriteLine(dr["ID"] + "," + dr["vName"] + "," + dr["iAge"]);
    }
    sw.Close();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
    Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载
    Response.WriteFile(name); // 把文件流发送到客户端
    Response.End();

    重点为红色标记处! 

     ------

    string strFile = "FileName" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv";

            //这一部分替换为你从DataTable/GridView获取的内容
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("id,name");
            sb.AppendLine("1,邹俊才");
            sb.AppendLine("2,才俊邹");
            sb.AppendLine("3,Jon");

            StringWriter sw = new StringWriter(sb);
            sw.Close();

            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", strFile));
            Response.Charset = "gb2312";
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.Write(sw);
            Response.Flush();
            Response.End();
     

  • 相关阅读:
    node的function函数和路由代码的小例子
    关于node回调函数中同步和异步操作的理解
    node初学
    CTF知识点总结(二)
    知识图谱
    论文笔记 无监督与混合IDS
    CTF知识点总结(一)
    论文笔记 网络安全图谱以及溯源算法
    攻防世界 wtf.sh-150
    攻防世界 Web_php_wrong_nginx_config
  • 原文地址:https://www.cnblogs.com/blsong/p/1699592.html
Copyright © 2011-2022 走看看