protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=Productor.doc");
        Response.ContentType = "application/msword";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        BindData();
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
    }
这段程序所要实现的功能我清楚,但是很多具体实现的细节不理解,从StringWriter sw = new StringWriter();这一行开始我就晕了。。请哪位好心人帮我推敲推敲,不胜感激!BindData()是将GridView与数据库绑定的函数,如下:
private void BindData()
    {
        String strConnection = "provider=Microsoft.Jet.OleDb.4.0;data source=F:\\ASPXROOT\\WebSite1\\App_Data\\Northwind.mdb;";
        OleDbConnection objConnection = new OleDbConnection(strConnection);
        String strSQL = "SELECT * FROM 产品";
        OleDbCommand objCommand = new OleDbCommand(strSQL,objConnection);
        objConnection.Open();
        GridView1.DataSource = objCommand.ExecuteReader();
        GridView1.DataBind();
        objConnection.Close();
    }

解决方案 »

  1.   

    RenderControl方法需要HtmlTextWriter 类型的参数而创建HtmlTextWriter 需要StringWriter 所以就这样了。。
      

  2.   

    那为什么AllowPage开始是false;最后又设置成true
      

  3.   

    那为什么AllowPage开始是false;最后又设置成true
      

  4.   

    楼主  这段代码的意思是  把AllowPage开始是false 则不分页  导出GridView1的所有数据  (不分页的)
    再设置GridView1分页  前台要展现分页的。  这种是最山寨的导出word方法。
      

  5.   


    在导出gridview 时要取消分页  等导出结束后 要恢复 gridview 分页,要不页面中的gridview 显示数据时就不分页了
      

  6.   

    //新建一个用来写入字符串的 System.IO.StringWriter 对象StringWriter sw = new StringWriter();//新建一个 将文本写入到服务器控件输出流的 一个对象,将 htw里面的内容写入到 sw 对象中HtmlTextWriter htw = new HtmlTextWriter(sw);
     
    //由于页面Gridview有分页 所以要 取消分页 重新绑定GridView  这要导出的才是 GridView中的所有内容 GridView1.AllowPaging = false;
      BindData();//将gridview中的内容输入到所提供的 htw 对象中  GridView1.RenderControl(htw);
      Response.Write(sw.ToString());
      Response.End();
    //要重新恢复分页  要不页面中的GridView 显示的内容将 不分页  GridView1.AllowPaging = true;
      

  7.   

    //新建一个用来写入字符串的 System.IO.StringWriter 对象StringWriter sw = new StringWriter();//新建一个 将文本写入到服务器控件输出流的 一个对象,将 htw里面的内容写入到 sw 对象中HtmlTextWriter htw = new HtmlTextWriter(sw);
     
    //由于页面Gridview有分页 所以要 取消分页 重新绑定GridView 这要导出的才是 GridView中的所有内容 GridView1.AllowPaging = false;
      BindData();//将gridview中的内容输入到所提供的 htw 对象中 
    GridView1.RenderControl(htw);
      Response.Write(sw.ToString());
      Response.End();
    //要重新恢复分页 要不页面中的GridView 显示的内容将 不分页
     GridView1.AllowPaging = true;
     
      

  8.   


    //新建一个 将文本写入到服务器控件输出流的 一个对象,将 htw里面的内容写入到 sw 对象中HtmlTextWriter htw = new HtmlTextWriter(sw);
    我怎么感觉是将sw的内容写入到htw里啊,求解释