如何将GridView控件数据导出到word和Excel

来源:互联网 发布:centos 7 极点五笔 编辑:程序博客网 时间:2024/04/29 14:55

  private void ExportGridView(GridView gridview, string fileType, string fileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        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);
        gridview.RenderControl(hw);
        //new ClientScriptManager().RegisterForEventValidation(new PostBackOptions());
        Response.Write(tw.ToString());
        Response.End();
        //fileType is "application/ms-word" and "application/ms-excel"
        // ExportGridView(this.GridView1, "application/ms-excel", "aa.xls");
        // ExportGridView(this.GridView1,"application/ms-word", "Word.doc"));
    }
    //必须列出该方法
    public override void VerifyRenderingInServerForm(Control control)
    {
    }