Need Help Exporting Gridview to RTF file for download

Last post 10-01-2009 7:35 AM by stinkyjak. 6 replies.

Sort Posts:

  • Need Help Exporting Gridview to RTF file for download

    09-29-2009, 8:44 AM
    • Member
      37 point Member
    • stinkyjak
    • Member since 03-27-2009, 7:51 AM
    • Posts 77

    I need help exporting Gridview to RTF file for download.

    I need a way to generate a report and export the results to a rtf file. I have found a lot of how-to's on exporting to PDF but I can't find anything on rtf.


    Please help me by example or point me to a good resource.


    Thank you in advance.

    J

    Filed under:
  • Re: Need Help Exporting Gridview to RTF file for download

    09-29-2009, 8:57 AM
    Answer
  • Re: Need Help Exporting Gridview to RTF file for download

    09-29-2009, 8:57 AM
    • All-Star
      22,266 point All-Star
    • vik20000in
    • Member since 12-30-2005, 6:02 AM
    • Kolkata
    • Posts 3,464
    • TrustedFriends-MVPs

    Protected Sub Button1_Click(sender As Object, e As EventArgs)

    Response.Clear()
    Response.Buffer = True

    Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")

    Response.ContentEncoding = System.Text.Encoding.UTF7
    Response.ContentType = "application/vnd.word"
    Dim oStringWriter As New System.IO.StringWriter()
    Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
    Me.GridView1.RenderControl(oHtmlTextWriter)
    Response.Output.Write(oStringWriter.ToString())
    Response.Flush()
    Response.End()

    End Sub

    Vikram
    www.vikramlakhotia.com


    Please mark the answer if it helped you
  • Re: Need Help Exporting Gridview to RTF file for download

    09-30-2009, 5:35 AM
    • Member
      37 point Member
    • stinkyjak
    • Member since 03-27-2009, 7:51 AM
    • Posts 77

    Now I only need to know how to write more than one GridView (lets say Gridview1, GridView2 and GridView3), to the same file.


    Do I just repeat:


            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.RenderControl(hw);
            Response.Output.Write(sw.ToString());
    
            GridView2.AllowPaging = false;
            GridView2.DataBind();
            GridView2.RenderControl(hw);
            Response.Output.Write(sw.ToString());
    
            GridView3.AllowPaging = false;
            GridView3.DataBind();
            GridView3.RenderControl(hw);
            Response.Output.Write(sw.ToString());
    




    and

    Response.Flush(); Response.End();


    when done?

  • Re: Need Help Exporting Gridview to RTF file for download

    09-30-2009, 5:40 AM
    • Member
      37 point Member
    • stinkyjak
    • Member since 03-27-2009, 7:51 AM
    • Posts 77

    also,

    I had to include:

    <%@ Page EnableEventValidation="False" %>

    on the aspx page. Is there a way to change this validation to false in the code behind and then back to true when done?


  • Re: Need Help Exporting Gridview to RTF file for download

    09-30-2009, 5:52 AM
    Answer
    • All-Star
      58,324 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,330
    • TrustedFriends-MVPs
  • Re: Need Help Exporting Gridview to RTF file for download

    10-01-2009, 7:35 AM
    • Member
      37 point Member
    • stinkyjak
    • Member since 03-27-2009, 7:51 AM
    • Posts 77

    Thanks everyone. Those links provided great information.

    This was also needed....


        protected void ExportWord_Button_Click(object sender, EventArgs e)
        {   
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
            Response.Charset = "";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252");
            Response.ContentType = "application/vnd.ms-word ";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
    
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    
    
    
    
    
    
    ///  was used on the aspx page, but wish there was a way to set/reset this in the code behind
    <%@ Page EnableEventValidation="False" 


Page 1 of 1 (7 items)