Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Thursday, July 15, 2010

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 !!!!!

15 comments:

  1. 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

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

    ReplyDelete
  3. 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();

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

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. 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?

    ReplyDelete
    Replies
    1. Hello faizan,

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

      Delete
  7. 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

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

    insead of downloading..

    ReplyDelete
  9. The code is not working when the html is having images

    ReplyDelete
  10. 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);
    }

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

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

    ReplyDelete
    Replies
    1. 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

      Delete
  13. @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.

    ReplyDelete