输出GridView到Word中编辑或打印

  通过Response对象输出数据,在输出的过程中设置输出的类型,从而实现将GridView控件中显示的数据导入到Word文档。

 protected void btnPut_Click(object sender, EventArgs e)
    {
        //添加HTTP头信息
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("report.doc", Encoding.UTF8).ToString());
        //修改输出流的MIME类型
        Response.ContentType = "application/ms-word";      
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

// 确认在运行时为制定的ASP.NET服务控件呈现<form id="form1" runat="server">标记
public override void VerifyRenderingInServerForm(Control control) { }

 

posted @ 2012-12-26 14:53  赤日枫叶  阅读(182)  评论(0编辑  收藏  举报