Transferring text from ASP.NET web form to a Word doc without HTML tags

Vrik22
Vrik22 used Ask the Experts™
on
How to render only text into a word document using asp.net web forms ?

I am using the below code , but, it is rendering with <html> tags :

 
Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/vnd.word";
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.AddHeader("Content-Disposition", "attachment;filename=xyz.doc");
                Response.Charset = "";
                EnableViewState = false;
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                oStringWriter.Write(txtDetails.Text);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                PanelControlId.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                HttpContext.Current.ApplicationInstance.CompleteRequest();

Open in new window


I want only text to be rendered in word and not with html tags. Please help .
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Director of Technology Development, IPM
Commented:
You should be able to use regular expressions to remove them.
Add the following to the top of your file:
using System;
using System.Text.RegularExpressions;

Open in new window

and then call this function:
    public string RemoveHTML(string strSource)
    {
        return Regex.Replace(strSource, "<.*?>", string.Empty);
    }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial