普通导出

来源:互联网 发布:阿里面试算法题 编辑:程序博客网 时间:2024/06/10 00:15

    #region 导出EXCEL类
    public void toExcel(GridView grdTemp)
    {
        grdTemp.AllowPaging = false;   //设置不能分页
        grdTemp.AllowSorting = false;
        DataTable table = bindDate();
        grdTemp.DataSource = table;  //重新绑定数据源
        grdTemp.GridLines = GridLines.Both;
        grdTemp.DataBind();

        //常规导出方法

        System.IO.StringWriter SW = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
        grdTemp.RenderControl(HTW);

        //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
        Response.Buffer = true;
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application nd.ms-excel";

        string str_day = System.DateTime.Now.ToShortDateString().ToString().Trim();
        //string fileName = "超期数据";
        Response.Charset = "gb2312";
        // Response.Charset = "Unicode";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.AddHeader("Content-Disposition", "attachment;filename=" + str_day + ".xls");
        Response.Write(SW.ToString());
        Response.Flush();
        Response.Close();
    }
    #endregion
    //GridView中引发异常,需重载此方法
    public override void VerifyRenderingInServerForm(Control control)
    {

    }

原创粉丝点击