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);
        }
  • 相关阅读:
    数据库设计范式
    SQL 触发器
    SQL查询上月和本月两个月的语句
    angularjs学习总结 详细教程
    指令
    重复 HTML 元素
    AngularJS 数据绑定
    AngularJS 指令.
    AngularJS 数组
    AngularJS 对象
  • 原文地址:https://www.cnblogs.com/bandy/p/1781382.html
Copyright © 2011-2022 走看看