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();
    }
  • 相关阅读:
    原生小程序 自定义封装组件
    H5 es6 foreach使用
    原生小程序底部弹出层动画过渡
    vue 动画滑动
    H5 textarea高度自适应
    关于Java日期加减,并且比较大小的方法
    activiti多实例如何配置
    常用类——Date——Calendar
    常用类-String
    Wrapper-装箱和拆箱
  • 原文地址:https://www.cnblogs.com/ecosu/p/4285769.html
Copyright © 2011-2022 走看看