Convert HTML to Word Document using ASP.Net (C#)

Hello Frd's

Find Below code to Convert HTML to Word Document using ASP.Net (C#).

 HttpContext.Current.Response.Clear();  
     HttpContext.Current.Response.Charset = "";  
     HttpContext.Current.Response.ContentType = "application/msword";  
     string strFileName = "GenerateDocumentfgfgfg" + ".doc";  
     HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);  
     StringBuilder strHTMLContent = new StringBuilder();  
     strHTMLContent.Append(RadEditor1.Content); 
     -- RadEditor1.Content is HTML Text  I m taking it from editor.
 
     HttpContext.Current.Response.Write(strHTMLContent);  
     HttpContext.Current.Response.End();  
     HttpContext.Current.Response.Flush();  


If you have any question write in comment box.


Enjoy !!!!!

Posted in |

15 comments:

  1. Talha Ashfaque Khan Says:

    Good one::

    There is more to do after Converting Word to HTML.
    Check this:
    http://www.codeshode.com/2011/07/convert-word-document-to-html-and.html

  2. Rajashekhar Reddy Dumala Says:

    Hi,
    How can i save this file to a folder path on server, instead of downloading.

  3. Santhakumar Says:

    Hi,

    i tried to convert html table to attachment of email(word Docs) but it shows unexpected error. here i put on my code.

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/msword";
    string strFileName = "GenerateDocument" + ".doc";
    HttpContext.Current.Response.AddHeader("Content-Disposition","inline;filename=" + strFileName);
    StringBuilder strHTMLContent = new StringBuilder();
    strHTMLContent.Append(HealthCheckForm.Content);

    HttpContext.Current.Response.Write(strHTMLContent);
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();

  4. Sasi Says:

    Thanks...I used this in my code, working

  5. Steffey Allyson Says: This comment has been removed by the author.
  6. faizan123 Says:

    i used your code, but i am getting a doc file with text and html tags. what should i do to convert those tags into formatting?

  7. Ketan Patel Says:

    Hello faizan,

    Sorry I didnt get what you want to say. Can you expalin your problem in details with your code ?

  8. kavithakesavan Says:

    Hi
    I have a aspx page with more than 10 different controls an d auser control too.How i can i render a entite page as word instead of rendering grid or appendeing to string builder..pls help

  9. Biplov sarkar Says:

    i want to save the file in Server.MapPath("../Template/temp1.rtf").

    insead of downloading..

  10. Johney Bravo Says:

    The code is not working when the html is having images

  11. alex smith Says:

    hi all,

    I;m currently working on the same issue and finding sample codes by Google, I copied the code but it doesn't work for ,can someone here help me out? Check the code snippet below, and this method is publisded on -
    http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Program-Guide/How-to-Convert-HTML-to-Word.html


    static void Main(string[] args)
    {
    Document document = new Document();

    document.LoadFromFile(@"D:\test.html",FileFormat.Html,XHTMLValidationType.None);
    document.SaveToFile("test.doc", FileFormat.Doc);
    }

  12. Anonymous Says:

    if i want to add a watermark how would i do that with this code.

  13. Jyoti Malik Says:

    can anyone tell from where you are getting "RadEditor1.Content" ?

  14. Lakpa Sherpa Says:

    Thats a telerik component.
    You can pass HTML instead of RadEditor1.Content

    Although above method works for desktop, it doesn't work properly in mobile device and IPAD will open it in Safari, thus you can't edit.

    Better to use third party tools. E.g Telerik

  15. Ketan Patel Says:

    @Jyti Malik.
    I have used Rad Editor (Telerik controls) in UI.

    Here you have to pass HTML if you don't have a same control.

Post a Comment