zoukankan      html  css  js  c++  java
  • ASP.NET GridView导出Excel

    导出GridView时候没有用控件,直接用流。
    在多个地方需要导出Excel,出现一个问题,有的地方导出的中文正常,但是有的地方导出的中文会出现乱码的情况。查找网上相关资料,可能问题发现是编码问题。我原来输出流字符集用的是Encoding.Default,将输出流字符集改成UTF-8仍然会有问题。
    查找资料发现在导出的时候加上如下代码可解决问题:

    Response.Write(“<meta http-equiv=Content-Type content=text/html;charset=GB2312>”);

    完整代码如下:

    public static void Export(Page page, GridView gv, string fileName)
    {
      page.Response.Buffer = true;
      page.Response.Clear();
      page.Response.ClearContent();
      page.Response.ClearHeaders();
      page.Response.Charset = “GB2312″;
      page.Response.ContentEncoding = System.Text.Encoding.GetEncoding(“GB2312″);
      page.Response.AddHeader(“Content-Disposition”, “attachment;filename=”+fileName+”.xls”);
      page.Response.Write(“<meta http-equiv=Content-Type content=text/html;charset=GB2312>”);
      page.Response.ContentType = “application/excel”;
      System.IO.StringWriter sw = new System.IO.StringWriter();
      HtmlTextWriter htw = new HtmlTextWriter(sw);
      gv.RenderControl(htw);
      page.Response.Write(sw.ToString());
      page.Response.End();
    }
  • 相关阅读:
    YTU 2481: 01字串
    smarty的自定义函数
    $_SERVER['SCRIPT_FLENAME']与__FILE__
    pdo中query()与prepare().execute()
    pdo中bindParam()与bindValue的区别
    pdo简介--错误与错误处理
    pdo的三个预定义类,PDO PDOStatement PDOException
    根目录 上级目录 当前目录
    反射机制(转)
    log4j 日志配置
  • 原文地址:https://www.cnblogs.com/ecosu/p/4285769.html
Copyright © 2011-2022 走看看