导出Excel Gridview

   导出Excel  Gridview    

///
    /// 定义导出Excel的函数
    ///
    ///
    ///
    private void Export(string FileType, string FileName)
    {


        GridView1.AllowPaging = false;    //导出分页的全部数据
        GridView1.AllowSorting = false;
        bind();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
        GridView1.AllowPaging = true;    //导出分页的全部数据
        GridView1.AllowSorting = true;
        bind();
    }
    ///
    /// 此方法必重写,否则会出错
    ///
    ///
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Export("application/ms-excel", "123.xls");

    }


还有由于是文件操作所以要引入名称空间IO和Text
using System.Drawing;
using System.IO;
using System.Text


上一篇:全选,选中删除功能的实现(含母版页-精简)


下一篇:GridView使用(转载自博客园)