您的位置:首页 > 其它

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

2009-11-11 21:55 537 查看
  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)
    {
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: