zoukankan      html  css  js  c++  java
  • 将DataGrid或GridView中的数据生成Excel

    代码
     void ToExcel(System.Web.UI.Control ctl, string FileName)
        {
            
    //设置网络输出流的HTTP字符集为UTF-8,Current为当前 HTTP 请求获取 HttpContext 对象
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding 
    = System.Text.Encoding.Default;
            
    //设置输出流HTTPMIME类型为excel
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            
    //将HTTP头添加到输出流
            HttpContext.Current.Response.AppendHeader("Content-Disposition""attachment;filename=" + "" + FileName + ".xls");
            
    //不保存该控件的视图状态
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter sw 
    = new System.IO.StringWriter();
            
    //将文本写入到输出流
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
            
    //将服务器控件的内容输出到HtmlTextWriter对象中
            ctl.RenderControl(hw);
            
    //StringWriter.ToString返回包含迄今为止写入到当前 StringWriter 中的字符的字符串
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();

        }

        
    //这个事件必须加
        public override void VerifyRenderingInServerForm(Control control)
        {
            
    //base.VerifyRenderingInServerForm(control);
        }
  • 相关阅读:
    openresty + gor+minio 集成
    openresty docker 镜像集成gor
    goreplay v1.1.0 支持pro特性docker 镜像
    编译goreplay v1.1.0 minio 集成支持
    编译goreplay v1.1.0 支持二进制协议捕捉
    goreplay v1.1.0 发布
    super-expressive 可以基于js 直接编写正则
    monio 的一些安全实践
    minio+ nginx rewrite 实现saas租户的个性化管理
    LDAP概念和原理介绍
  • 原文地址:https://www.cnblogs.com/bandy/p/1781382.html
Copyright © 2011-2022 走看看