locked
Microsoft Word RRS feed

  • Question

  • User1922250498 posted

    Hey there, I have Word 2007 and  asp.net 4.0   How can I get the contents of my web form into a specific word document?   thanks!

    I am also using c# and dynamic data and linq to sql. 

     

    Monday, November 15, 2010 5:18 PM

Answers

  • User1154043639 posted

    Since this is on the Render, its before the user gets to enter data into the fields.  Is it possible to have this fire off at a point after they submit the form?

    you can either use this.IsPostBack or

    <title>Snippet</title>

    bool print = false;
    protected void PrintButtonClick(object sender, EventArgs e)
    {
        print = true;
    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (print)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/msword";
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlTextWriter);
            Response.Write(sb.ToString());
            Response.End();
        }
        else
            base.Render(writer);
    }

    Is there a way to have word use a specific template that is pre-formatted?  Or perhaps build all this out as a open xml doc that is formatted in a particular way? 


    You can use seperate differently formatted print pages.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 16, 2010 4:23 PM

All replies

  • User-837620913 posted

    Try this blog post:

    http://geekswithblogs.net/vivek/archive/2006/09/26/92316.aspx

    Monday, November 15, 2010 5:25 PM
  • User1922250498 posted

    Ok, thanks for the link!   I followed those instructions and got the following error:

    control "myControl" must be placed inside a form tag with runat=server

     

    So with my site using a Master Page and then a page and this control being in a .ascx control on the page - i am not sure where to place the "form" tag with runat=server, as the form tag on my master page has runat=server, and my page(aspx) has no form tag, and my control (ascx) has no form tag.   

    Monday, November 15, 2010 5:50 PM
  • User-1809536021 posted

    include form tag.. check that.. 

    <body>
        <form id="form1" runat="server">
        <div>
        <uc:control runat="server" ID="mycontrol" />
        </div>
        </form>
        </body>

    Monday, November 15, 2010 8:33 PM
  • User1154043639 posted

    You can use page Render method.

    Example:

    <title>Snippet</title>

    public partial class Test : Page
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/msword";
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlTextWriter);
            Response.Write(sb.ToString());
            Response.End();            
        }
    }


    Tuesday, November 16, 2010 6:16 AM
  • User1922250498 posted

    As far as adding runat=server on the usercontrol = this is a dynamicdata website, so there actually is no <uc:control> tag.   Its all done via dynamic routing with httpcontext.  So instead the following is used:

     

    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true">
            <DataControls>
                <asp:DataControlReference ControlID="FormView1" />
            </DataControls>
        </asp:DynamicDataManager>

     

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       
            <ContentTemplate>       
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
                    HeaderText="List of validation errors" CssClass="DDValidator" />
                <asp:DynamicValidator runat="server" ID="DetailsViewValidator" ControlToValidate="FormView1" Display="None" CssClass="DDValidator" />

                <asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
                    OnItemCommand="FormView1_ItemCommand" OnItemInserted="FormView1_ItemInserted" RenderOuterTable="false">
                    <InsertItemTemplate>
                        <table id="detailsTable" cellpadding="6">  
                                        
                            <asp:DynamicEntity runat="server" Mode="Insert" />
                            <tr>
                                <td colspan="2">                                                       
                                   
                                </td>
                            </tr>
                        </table>
                    </InsertItemTemplate>
                </asp:FormView>
                <asp:LinqDataSource ID="DetailsDataSource" runat="server" EnableInsert="true" />           
            </ContentTemplate>
        </asp:UpdatePanel>

     

    Ok, so the page render does in fact, open word with all the fields in the document.   

    There are a coupel things that I still would like to know if are possible:

    Since this is on the Render, its before the user gets to enter data into the fields.  Is it possible to have this fire off at a point after they submit the form?

    Is there a way to have word use a specific template that is pre-formatted?  Or perhaps build all this out as a open xml doc that is formatted in a particular way? 

     

    Thanks for all the help, cheers!

    Tuesday, November 16, 2010 11:35 AM
  • User1154043639 posted

    Since this is on the Render, its before the user gets to enter data into the fields.  Is it possible to have this fire off at a point after they submit the form?

    you can either use this.IsPostBack or

    <title>Snippet</title>

    bool print = false;
    protected void PrintButtonClick(object sender, EventArgs e)
    {
        print = true;
    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (print)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/msword";
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlTextWriter);
            Response.Write(sb.ToString());
            Response.End();
        }
        else
            base.Render(writer);
    }

    Is there a way to have word use a specific template that is pre-formatted?  Or perhaps build all this out as a open xml doc that is formatted in a particular way? 


    You can use seperate differently formatted print pages.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 16, 2010 4:23 PM
  • User1922250498 posted

    Thanks that works perfectly!   Now I just need to find a godo way to make the print lok nice, cheers!

     

     

    Tuesday, November 16, 2010 6:53 PM