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();
    }
  • 相关阅读:
    static 续--
    [非原创]java 中static作用解析
    public/private/protected作用域
    三种排序方法(冒泡、选择、插入)
    SQLMAP自动注入(四)
    SQLMAP自动注入(三)—Injection,Detection,Techniques
    SQLMAP自动注入(二)-request,优化
    SQLMAP自动注入(一)
    SQL盲注
    SQL注入——猜测字段名称
  • 原文地址:https://www.cnblogs.com/ecosu/p/4285769.html
Copyright © 2011-2022 走看看