普通导出

本文介绍了一种使用ASP.NET中的GridView控件将数据导出到Excel文件的方法。该方法禁用了分页和排序功能,并通过StringWriter和HtmlTextWriter渲染控件内容。最终通过HttpResponse将内容发送给客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    #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)
    {

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值