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);
        }
  • 相关阅读:
    python 时间差计算
    NET Framework 4.5新特性 (一) 数据库的连接加密保护。
    某表含有N个字段超精简模糊查询方法
    清空javascript数组数据
    IIS无法连接LocalDb,怎么办?
    jquery 模糊查询对象属性
    解释杨中科随机数为什么会骗人?
    前端Js传递数组至服务器端
    javascript获取客户端默认打印机
    水晶报表注意的问题
  • 原文地址:https://www.cnblogs.com/bandy/p/1781382.html
Copyright © 2011-2022 走看看