Export Formatted HTML to Word Document in ASP.Net using C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

Dear All,

I have one form with the following fields as template and want users to update in the web form and print as word document for the below template.

Number: 4020-I-95

Attachment E

                 

1.0              Title:

…….. 

2.0              Condition:

This policy applies for…….  

3.0              Statement of Purpose:

To establish guidelines for………..

4.0              Definition:

4.1              Policy: is the …..

5.0              Policy:

5.1              The entire …

6.0              Procedure:

6.1              The amount …….

6.1.1        Symposium ….

6.1.1.1  Chairperson ….

7.0              Roles & Responsibilities:

7.1               

8.0              References:

8.1              Janet Brown handbook ….

8.2              ISMP

9.0              Attachments:

9.1              Attachment A: Evaluation Form for …….

Can you please suggest me how can I build the web form for the above Template.

Thanks in advance.

You are viewing reply posted by: pandeyism one year ago.
Posted one year ago

Hi irshad1231,

Take reference to the below sample and according to below code complete your requirement.

HTML

Title :
<br />
<asp:TextBox runat="server" ID="txtTitle" />
<br />
Condition:
<br />
<asp:TextBox runat="server" ID="txtCondition" />
<br />
Purpose:
<br />
<asp:TextBox runat="server" ID="txtPurpose" />
<br />
Policy :
<br />
<asp:TextBox runat="server" ID="txtPolicy" />
<br />
<asp:Button Text="ExportToWord" runat="server" OnClick="Export_To_Word" />

Code

C#

protected void Export_To_Word(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<HTML>");
    sb.Append("<head>");
    sb.Append(" <title></title>");
    sb.Append("<b>Attachment E</b>");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("<b>1.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title:</b>");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("" + txtTitle.Text + "");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("<b>2.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Condition:</b>");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("This policy applies for  " + txtCondition.Text + "");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("<b>3.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement of Purpose:</b>");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("To establish guidelines for  " + txtPurpose.Text + "");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("4<b>.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Definition:</b>");
    sb.Append("<br />");
    sb.Append("<br />");
    sb.Append("4.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Policy: is the " + txtPolicy.Text + "");
    sb.Append("</head>");
    sb.Append("</HTML>");
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
}

VB.Net

Protected Sub Export_To_Word(ByVal sender As Object, ByVal e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-word "
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<HTML>")
    sb.Append("<head>")
    sb.Append(" <title></title>")
    sb.Append("<b>Attachment E</b>")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("<b>1.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title:</b>")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("" & txtTitle.Text & "")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("<b>2.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Condition:</b>")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("This policy applies for  " & txtCondition.Text & "")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("<b>3.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement of Purpose:</b>")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("To establish guidelines for  " & txtPurpose.Text & "")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("4<b>.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Definition:</b>")
    sb.Append("<br />")
    sb.Append("<br />")
    sb.Append("4.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Policy: is the " & txtPolicy.Text & "")
    sb.Append("</head>")
    sb.Append("</HTML>")
    Response.Output.Write(sb.ToString())
    Response.Flush()
    Response.[End]()
End Sub

Screenshot